mirror of
https://github.com/koloml/philomena-tagging-assistant.git
synced 2026-09-22 20:09:39 +00:00
Added option to decorate tag links in forum posts
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import { namespaceCategories } from "$config/tags";
|
||||
|
||||
/**
|
||||
* Build the map containing both real tags and their aliases.
|
||||
*
|
||||
@@ -31,3 +33,52 @@ export function buildTagsAndAliasesMap(realAndAliasedTags: string[], realTags: s
|
||||
|
||||
return tagsAndAliasesMap;
|
||||
}
|
||||
|
||||
const tagLinkRegExp = /\/tags\/(?<encodedTagName>[^/?#]+)/;
|
||||
|
||||
/**
|
||||
* List of encoded characters from Philomena.
|
||||
*
|
||||
* @see https://github.com/philomena-dev/philomena/blob/6086757b654da8792ae52adb2a2f501ea6c30d12/lib/philomena/slug.ex#L52-L57
|
||||
*/
|
||||
const slugEncodedCharacters: Map<string, string> = new Map([
|
||||
['-dash-', '-'],
|
||||
['-fwslash-', '/'],
|
||||
['-bwslash-', '\\'],
|
||||
['-colon-', ':'],
|
||||
['-dot-', '.'],
|
||||
['-plus-', '+'],
|
||||
]);
|
||||
|
||||
/**
|
||||
* Decode the tag name from its link path.
|
||||
*
|
||||
* @param tagLink Full or partial link to the tag.
|
||||
*
|
||||
* @return Tag name or NULL if function is failed to recognize the link as tag-related link.
|
||||
*/
|
||||
export function decodeTagNameFromLink(tagLink: string): string | null {
|
||||
tagLinkRegExp.lastIndex = 0;
|
||||
|
||||
const result = tagLinkRegExp.exec(tagLink);
|
||||
const encodedTagName = result?.groups?.encodedTagName;
|
||||
|
||||
if (!encodedTagName) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return decodeURIComponent(encodedTagName)
|
||||
.replaceAll(/-[a-z]+-/gi, match => slugEncodedCharacters.get(match) ?? match)
|
||||
.replaceAll('-', ' ');
|
||||
}
|
||||
|
||||
/**
|
||||
* Try to resolve the category from the tag name.
|
||||
*
|
||||
* @param tagName Name of the tag.
|
||||
*/
|
||||
export function resolveTagCategoryFromTagName(tagName: string): string | null {
|
||||
const namespace = tagName.split(':')[0];
|
||||
|
||||
return namespaceCategories.get(namespace) ?? null;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ import CacheableSettings from "$lib/extension/base/CacheableSettings";
|
||||
|
||||
interface TagSettingsFields {
|
||||
groupSeparation: boolean;
|
||||
replaceLinks: boolean;
|
||||
}
|
||||
|
||||
export default class TagSettings extends CacheableSettings<TagSettingsFields> {
|
||||
@@ -13,7 +14,15 @@ export default class TagSettings extends CacheableSettings<TagSettingsFields> {
|
||||
return this._resolveSetting("groupSeparation", true);
|
||||
}
|
||||
|
||||
async resolveReplaceLinks() {
|
||||
return this._resolveSetting("replaceLinks", false);
|
||||
}
|
||||
|
||||
async setGroupSeparation(value: boolean) {
|
||||
return this._writeSetting("groupSeparation", Boolean(value));
|
||||
}
|
||||
|
||||
async setReplaceLinks(value: boolean) {
|
||||
return this._writeSetting("replaceLinks", Boolean(value));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user