Translit slug


  • Plugin & Theme Dev

    Are there any possibility to use transliteration slug during the creation of categories and posts? I do not know what with other languages, but with Russian is not very good search engines work.


  • GNU/Linux Admin

    Can you post an example of a slug that does not work well?


  • Plugin & Theme Dev

    @julian /forum/category/134/вероятно-нехорошо-для-поисковиков


  • Plugin & Theme Dev

    @julian It Could Be



  • @sergej-saveljev said:

    /forum/category/134/вероятно-нехорошо-для-поисковиков`

    You can still read this.
    /forum/category/134/probably-not-good-for-search-engines

    Do you want to remove /forum/category/134/ from the URL?


  • Plugin & Theme Dev

    @nhl.pl no.


  • GNU/Linux Admin

    So what is it that you need to change?


  • Plugin & Theme Dev

    @julian need, when create a categories and posts, translit вероятно-нехорошо-для-поисковиков to verojatno-nexorosho-dlja-poiskovikov



  • It can be done with iconv-lite.

    https://github.com/ashtuchkin/iconv-lite



  • @nhl.pl Can you tell a bit more ?



  • @julian Problem with latin and cyrillic url is this, when you copy / pasting link it looks like
    %D0%B3%D0%BE%D1%81%D1%82%D0%B8%D0%BD%D0%B0%D1%8F



  • In the near future it's going to be used in nodebb-plugin-import.


  • GNU/Linux Admin

    Oh I see... to use Latin transliteration for the url slug.

    A plugin can conceivably be written for this, expressly for Russian languages (and possibly any other languages that could benefit from this kind of service), listening for the topic creation hook, and changing the slug afterwards.


  • 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!


Log in to reply
 

Suggested Topics

| |