From fbcb02cbbbc73cf86912a78bd1659f6664ed064c Mon Sep 17 00:00:00 2001 From: Mara-Li Date: Fri, 26 Jan 2024 22:02:07 +0100 Subject: [PATCH] fix: allow multiple option for slugify (#287) Close #285 --- src/conversion/links.ts | 36 ++++++++++++++++++++++-------------- src/i18n/locales/en.json | 3 +++ src/i18n/locales/fr.json | 3 +++ src/settings.ts | 15 ++++++++++----- src/settings/interface.ts | 4 ++-- 5 files changed, 40 insertions(+), 21 deletions(-) diff --git a/src/conversion/links.ts b/src/conversion/links.ts index 6f5f6244..742380e1 100644 --- a/src/conversion/links.ts +++ b/src/conversion/links.ts @@ -7,7 +7,7 @@ import { LinkedNotes, MultiProperties, } from "../settings/interface"; -import {isAttachment, noTextConversion} from "../utils/data_validation_test"; +import { isAttachment, noTextConversion } from "../utils/data_validation_test"; import { createRelativePath, linkIsInFormatter, textIsInFrontmatter } from "./file_path"; import { replaceText } from "./find_and_replace_text"; @@ -202,19 +202,30 @@ function createMarkdownLinks(fileName: string, isEmbed: string, altLink: string, const markdownName = !isAttachment(fileName.trim()) ? fileName.replace(/#.*/, "").trim() + ".md" : fileName.trim(); - const anchorMatch = fileName.match(/(#.*)/); - let anchor = anchorMatch ? anchorMatch[0].replaceAll(" ", "%20") : ""; + const anchorMatch = fileName.match(/(#.*)/); + let anchor = anchorMatch ? anchorMatch[0] : null; const encodedURI = encodeURI(markdownName); - if (settings.conversion.links.slugify && anchorMatch) { - const slugified = slugify(anchorMatch[0], { lower: true, strict: true }); - anchor = slugified.trim().length > 0 ? slugified : anchorMatch[0]; - if (anchor.length > 0) - anchor = `${anchor}`; - } + anchor = slugifyAnchor(anchor, settings); return `${isEmbed}[${altLink}](${encodedURI}${anchor})`; } +function slugifyAnchor(anchor: string | null, settings: GitHubPublisherSettings) { + const slugifySetting = typeof(settings.conversion.links.slugify) === "string" ? settings.conversion.links.slugify : "disable"; + if (anchor && slugifySetting !== "disable") { + switch (settings.conversion.links.slugify) { + case "lower": + return anchor.toLowerCase().replaceAll(" ", "-"); + case "strict": + return slugify(anchor, { lower: true, strict: true }); + default: + return anchor; + } + + } + return anchor?.replaceAll(" ", "%20") ?? ""; +} + /** * Add alt text to links * @param {string} link the link to add alt text to @@ -292,11 +303,8 @@ export async function convertToInternalGithub( let newLink = link.replace(regToReplace, pathInGithubWithAnchor); //strict replacement of link if (link.match(/\[.*\]\(.*\)/)) { if (linkedFile.linked.extension === "md" && !linkedFile.linked.name.includes("excalidraw")) { - const slugified = slugify(anchor, { lower: true, strict: true}); - anchor = settings.conversion.links.slugify && slugified.trim().length > 0 ? slugified : anchor; - if (anchor.length > 0) - anchor = `#${anchor}`; - pathInGithub = `${pathInGithub.replaceAll(" ", "%20")}.md${anchor}`; + anchor = slugifyAnchor(anchor, settings); + pathInGithub = `${pathInGithub.replaceAll(" ", "%20")}.md#${anchor}`; //probably useless // pathInGithub = pathInGithub.replace(/(\.md)?(#.*)/, ".md$2"); pathInGithub = !pathInGithub.match(/(#.*)/) && !pathInGithub.endsWith(".md") ? diff --git a/src/i18n/locales/en.json b/src/i18n/locales/en.json index fcac11d1..cf4f3477 100644 --- a/src/i18n/locales/en.json +++ b/src/i18n/locales/en.json @@ -182,6 +182,9 @@ }, "slugify": { "desc": "Standardize the slug of anchor links (pointing to heading title). Transform the slug into all lower case. Replace space with hyphen. Applicable only for anchor links in markdown link syntax.", + "disable": "Disable", + "lower": "", + "strict": "Convert all to alphanumeric and dashes, including unicode and non latin languages.", "title": "Sluglify anchor in markdown links" }, "title": "Links", diff --git a/src/i18n/locales/fr.json b/src/i18n/locales/fr.json index 924dd22c..c54e3077 100644 --- a/src/i18n/locales/fr.json +++ b/src/i18n/locales/fr.json @@ -182,6 +182,9 @@ }, "slugify": { "desc": "Normaliser le lien (slug) des liens d'ancrage (pointant vers le titre de la rubrique). Transforme le texte en minuscules. Remplace l'espace par un tiret. Applicable uniquement aux liens d'ancrage dans la syntaxe de lien markdown.", + "disable": "Désactiver", + "lower": "Convertit en minuscules et les espaces en tirets", + "strict": "Convertit tout en alphanumérique et tirets, y compris l'unicode et les langues non latine.", "title": "Slugifier l'ancre des liens markdown" }, "title": "Liens", diff --git a/src/settings.ts b/src/settings.ts index 1d1b91fd..9ced7957 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -733,16 +733,21 @@ export class GithubPublisherSettingsTab extends PluginSettingTab { this.renderSettingsPage("text-conversion"); }); }); - + const slugifySetting = typeof(textSettings.links.slugify)=="boolean" ? textSettings.links.slugify ? "strict" : "disable" : textSettings.links.slugify; if (textSettings.links.wiki || textSettings.links.internal) { new Setting(this.settingsPage) .setName(i18next.t("settings.conversion.links.slugify.title")) .setDesc(i18next.t("settings.conversion.links.slugify.desc")) - .addToggle((toggle) => { - toggle - .setValue(textSettings.links.slugify) + .addDropdown((dropdown) => { + dropdown + .addOptions({ + disable: i18next.t("settings.conversion.links.slugify.disable"), + strict: i18next.t("settings.conversion.links.slugify.strict"), + lower: i18next.t("settings.conversion.links.slugify.lower"), + }) + .setValue(slugifySetting) .onChange(async (value) => { - textSettings.links.slugify = value; + textSettings.links.slugify = ["disable", "strict", "lower"].includes(value) ? value as "disable" | "strict" | "lower" : "disable"; await this.plugin.saveSettings(); }); }); diff --git a/src/settings/interface.ts b/src/settings/interface.ts index 60111d95..d99fd870 100644 --- a/src/settings/interface.ts +++ b/src/settings/interface.ts @@ -116,7 +116,7 @@ export interface GitHubPublisherSettings { internal: boolean; unshared: boolean; wiki: boolean; - slugify: boolean; + slugify: "disable" | "strict" | "lower" | boolean; } } embed: { @@ -278,7 +278,7 @@ export const DEFAULT_SETTINGS: Partial = { internal: false, unshared: false, wiki: false, - slugify: false, + slugify: "disable", }, }, embed: {