Translit slug



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


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


Log in to reply
 

Suggested Topics

| |