Add slugify and duplicateslugs operators

Fixes #3379
This commit is contained in:
jeremy@jermolene.com
2020-05-09 15:54:44 +01:00
parent 33d973fb91
commit 4966f6ab62
6 changed files with 140 additions and 0 deletions
+36
View File
@@ -0,0 +1,36 @@
/*\
title: $:/core/modules/filters/duplicateslugs.js
type: application/javascript
module-type: filteroperator
Filter function for [duplicateslugs[]]
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
/*
Export our filter function
*/
exports.duplicateslugs = function(source,operator,options) {
var slugs = Object.create(null), // Hashmap by slug of title, replaced with "true" if the duplicate title has already been output
results = [];
source(function(tiddler,title) {
var slug = options.wiki.slugify(title);
if(slug in slugs) {
if(slugs[slug] !== true) {
results.push(slugs[slug]);
slugs[slug] = true;
}
results.push(title);
} else {
slugs[slug] = title;
}
});
return results;
};
})();
+23
View File
@@ -0,0 +1,23 @@
/*\
title: $:/plugins/tiddlywiki/static/filters/slugify.js
type: application/javascript
module-type: filteroperator
Filter operator for slugifying a tiddler title
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
exports.slugify = function(source,operator,options) {
var results = [];
source(function(tiddler,title) {
results.push(options.wiki.slugify(title));
});
return results;
};
})();
+25
View File
@@ -1503,5 +1503,30 @@ exports.doesPluginInfoRequireReload = function(pluginInfo) {
}
};
exports.slugify = function(title,options) {
var tiddler = this.getTiddler(title),
slug;
if(tiddler && tiddler.fields.slug) {
slug = tiddler.fields.slug;
} else {
slug = $tw.utils.transliterate(title.toString().toLowerCase()) // Replace diacritics with basic lowercase ASCII
.replace(/\s+/g,"-") // Replace spaces with -
.replace(/[^\w\-\.]+/g,"") // Remove all non-word chars except dash and dot
.replace(/\-\-+/g,"-") // Replace multiple - with single -
.replace(/^-+/,"") // Trim - from start of text
.replace(/-+$/,""); // Trim - from end of text
}
// If the resulting slug is blank (eg because the title is just punctuation characters)
if(!slug) {
// ...then just use the character codes of the title
var result = [];
$tw.utils.each(title.split(""),function(char) {
result.push(char.charCodeAt(0).toString());
});
slug = result.join("-");
}
return slug;
};
})();