mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2026-09-20 08:40:42 +00:00
@@ -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;
|
||||
};
|
||||
|
||||
})();
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
})();
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
})();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user