Skip to content

Commit

Permalink
fix: allow multiple option for slugify (#287)
Browse files Browse the repository at this point in the history
Close #285
  • Loading branch information
Mara-Li authored Jan 26, 2024
1 parent a9c32e6 commit fbcb02c
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 21 deletions.
36 changes: 22 additions & 14 deletions src/conversion/links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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") ?
Expand Down
3 changes: 3 additions & 0 deletions src/i18n/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 3 additions & 0 deletions src/i18n/locales/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
15 changes: 10 additions & 5 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
Expand Down
4 changes: 2 additions & 2 deletions src/settings/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export interface GitHubPublisherSettings {
internal: boolean;
unshared: boolean;
wiki: boolean;
slugify: boolean;
slugify: "disable" | "strict" | "lower" | boolean;
}
}
embed: {
Expand Down Expand Up @@ -278,7 +278,7 @@ export const DEFAULT_SETTINGS: Partial<GitHubPublisherSettings> = {
internal: false,
unshared: false,
wiki: false,
slugify: false,
slugify: "disable",
},
},
embed: {
Expand Down

0 comments on commit fbcb02c

Please sign in to comment.