• Plugin & Theme Dev

    @julian Made, thank you!

  • Plugin & Theme Dev

    But i not find topic edit method, where is he?

  • GNU/Linux Admin

    Likely PostTools.edit, but this just re-slugifies the topic, so you may want to invoke the db directly:

    Topics.setTopicField(tid, 'slug', tid + '/' + transliteratedTitle, callback);

  • Plugin & Theme Dev

    @julian you misunderstood me.

    I added new fireHook in file ./src/topics/create.js:

        Topics.create = function(data, callback) {
    		plugins.fireHook('filter:topic.create', data, function(err, data){
    			if (err) {
    				return callback(err);
    			}
    			
    			var uid = data.uid,
    				title = data.title,
    				cid = data.cid,
    				tags = data.tags,
    				slugw = data.slug ? data.slug : title;
    
    			db.incrObjectField('global', 'nextTid', function(err, tid) {
    				if (err) {
    					return callback(err);
    				}
    
    				var slug = utils.slugify(slugw),
    					timestamp = Date.now();
    
    				if (!slug.length) {
    					return callback(new Error('[[error:invalid-title]]'));
    				}
    
    				slug = tid + '/' + slug;
    
    				var topicData = {
    					'tid': tid,
    					'uid': uid,
    					'cid': cid,
    					'mainPid': 0,
    					'title': title,
    					'slug': slug,
    					'timestamp': timestamp,
    					'lastposttime': 0,
    					'postcount': 0,
    					'viewcount': 0,
    					'locked': 0,
    					'deleted': 0,
    					'pinned': 0
    				};
    
    				if (data.thumb) {
    					topicData.thumb = data.thumb;
    				}
    
    				db.setObject('topic:' + tid, topicData, function(err) {
    					if (err) {
    						return callback(err);
    					}
    
    					async.parallel([
    						function(next) {
    							db.sortedSetsAdd([
    								'topics:tid',
    								'cid:' + cid + ':tids',
    								'cid:' + cid + ':uid:' + uid + ':tids'
    							], timestamp, tid, next);
    						},
    						function(next) {
    							user.addTopicIdToUser(uid, tid, timestamp, next);
    						},
    						function(next) {
    							db.incrObjectField('category:' + cid, 'topic_count', next);
    						},
    						function(next) {
    							db.incrObjectField('global', 'topicCount', next);
    						},
    						function(next) {
    							Topics.createTags(tags, tid, timestamp, next);
    						}
    					], function(err) {
    						if (err) {
    							return callback(err);
    						}
    						plugins.fireHook('action:topic.save', topicData);
    						callback(null, tid);
    					});
    				});
    			});
    			
    		});
    	};
    

    And in plugin used:

    (function(module) {
    	"use strict";
    
        var mod = {},
    		translit = require('translitit-cyrillic-russian-to-latin');
    	
    	
    	mod.topicCreate = function(data, callback) {
    		
    		data.slug = translit(data.title);
    		
    		callback(null, data);
    		
    	};
    
    module.exports = mod;
        
    }(module));
    

    translitit-cyrillic-russian-to-latin my fork on github.

  • Plugin & Theme Dev

    And I can not find a method of editing a topic in topics folder, to add the appropriate fireHook.

  • Plugin & Theme Dev

    @julian yes, it PostTools.edit 🙂

  • Plugin & Theme Dev

    In file ./src/postTools.js:

        PostTools.edit = function(data, callback) {
    		var options = data.options || {},
    			title = data.title.trim();
    
    		async.waterfall([
    			function (next) {
    				privileges.posts.canEdit(data.pid, data.uid, next);
    			},
    			function(canEdit, next) {
    				if (!canEdit) {
    					return next(new Error('[[error:no-privileges]]'));
    				}
    				posts.getPostData(data.pid, next);
    			},
    			function(postData, next) {
    				postData.content = data.content;
    				plugins.fireHook('filter:post.edit', {post: postData, uid: data.uid}, next);
    			}
    		], function(err, result) {
    			if (err) {
    				return callback(err);
    			}
    
    			var postData = result.post;
    			async.parallel({
    				post: function(next) {
    					var d = {
    						edited: Date.now(),
    						editor: data.uid,
    						content: postData.content
    					};
    					if (data.handle) {
    						d.handle = data.handle;
    					}
    					posts.setPostFields(data.pid, d, next);
    				},
    				topic: function(next) {
    					var tid = postData.tid;
    					async.parallel({
    						cid: function(next) {
    							topics.getTopicField(tid, 'cid', next);
    						},
    						isMain: function(next) {
    							posts.isMain(data.pid, next);
    						}
    					}, function(err, results) {
    						plugins.fireHook('filter:topic.edit', {post: postData, topic: results, title: title}, function(err, res) {
    							if (err) {
    								return next(err);
    							}
    							
    							var postData = res.post,
    								results = res.topic,
    								slug = res.slug || title;
    							
    							options.tags = options.tags || [];
    
    							if (!results.isMain) {
    								return next(null, {
    									tid: tid,
    									cid: results.cid,
    									isMainPost: false
    								});
    							}
    
    							var topicData = {
    								tid: tid,
    								cid: results.cid,
    								uid: postData.uid,
    								mainPid: data.pid,
    								title: title,
    								slug: tid + '/' + utils.slugify(slug)
    							};
    							if (options.topic_thumb) {
    								topicData.thumb = options.topic_thumb;
    							}
    
    							db.setObject('topic:' + tid, topicData, function(err) {
    								plugins.fireHook('action:topic.edit', topicData);
    							});
    
    							topics.updateTags(tid, options.tags, function(err) {
    								if (err) {
    									return next(err);
    								}
    								topics.getTopicTagsObjects(tid, function(err, tags) {
    									next(err, {
    										tid: tid,
    										cid: results.cid,
    										uid: postData.uid,
    										title: validator.escape(title),
    										isMainPost: results.isMain,
    										tags: tags
    									});
    								});
    							});
    						});
    					});
    				},
    				postData: function(next) {
    					cache.del(postData.pid);
    					PostTools.parsePost(postData, next);
    				}
    			}, function(err, results) {
    				if (err) {
    					return callback(err);
    				}
    				postData.cid = results.topic.cid;
    				results.content = results.postData.content;
    
    				plugins.fireHook('action:post.edit', postData);
    				callback(null, results);
    			});
    		});
    	};
    

    The same method as for the creation of topics and everything is super!

  • Plugin & Theme Dev

    node-transliteration thanks @Mega

    npm install transliteration
    

    plugin index.js:

    (function(module) {
        "use strict";
    
        var mod = {},
            translit = require('transliteration').slugify;
        
        
        mod.topicCreate = function(data, callback) {
            
            data.slug = translit(data.title, {lowercase: true, separator: '_'});
            
            callback(null, data);
            
        };
    
        mod.topicEdit = function(data, callback) {
            
            data.slug = translit(data.title, {lowercase: true, separator: '_'});
            
            callback(null, data);
            
        };
    
    module.exports = mod;
        
    }(module));
    

  • eh.. How can I use it on my forum ?

  • Plugin & Theme Dev

    Please, anyone who is interested to join! https://github.com/NodeBB/NodeBB/issues/3097

    atomoc created this issue in NodeBB/NodeBB

    closed Slug transliteration #3097

  • Plugin & Theme Dev

    I also interested in slugs with transliteration.
    But, I think, It could be done from the plugin, main idea is to use hooks around create methods.

    Areas where It could be used:

    • Topic titles
    • Categories
    • User names

    I have 2 plugins in plans: utils - to reconstruct in-place slugs, for me It's easier to have lightweight utility, than very heavy import script; slugs - I'm also not happy with Cyrillic letters in urls 😉

    P.S.
    @julian slugs are really good candidate for core improvement 😉

Suggested Topics

| |