From 01f541afb075a22c17de5722c1c6452ef7ffdda1 Mon Sep 17 00:00:00 2001 From: Mara Date: Fri, 26 Jan 2024 20:51:59 +0100 Subject: [PATCH] fix: allow multiple option for slugify --- src/conversion/links.ts | 33 +++++++++++++++++++-------------- src/settings.ts | 15 ++++++++++----- src/settings/interface.ts | 4 ++-- 3 files changed, 31 insertions(+), 21 deletions(-) diff --git a/src/conversion/links.ts b/src/conversion/links.ts index 6f5f6244..12b0aa2f 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,27 @@ 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 = slugifyWithSettings(anchor, settings); return `${isEmbed}[${altLink}](${encodedURI}${anchor})`; } +function slugifyWithSettings(anchor: string | null, settings: GitHubPublisherSettings) { + if (anchor && settings.conversion.links.slugify !== "disable") { + switch (settings.conversion.links.slugify) { + case "lower": + return anchor.toLowerCase().replaceAll(" ", "-"); + case "strict": + return slugify(anchor, { lower: true, strict: true }); + } + + } + return anchor?.replaceAll(" ", "%20") ?? ""; +} + /** * Add alt text to links * @param {string} link the link to add alt text to @@ -292,11 +300,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 = slugifyWithSettings(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/settings.ts b/src/settings.ts index 1d1b91fd..5d029371 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: "Disable", + strict: "Strict (convert all to alphanumeric and dashes, including unicode and eastern languages)", + lower: "Lower (convert to lowercase and space to dashes)", + }) + .setValue(slugifySetting) .onChange(async (value) => { - textSettings.links.slugify = value; + textSettings.links.slugify = value as "disable" | "strict" | "lower"; 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: {