Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow multiple option for slugify #287

Merged
merged 6 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 19 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,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") ?? "";
}
Mara-Li marked this conversation as resolved.
Show resolved Hide resolved

/**
* Add alt text to links
* @param {string} link the link to add alt text to
Expand Down Expand Up @@ -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") ?
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 = value as "disable" | "strict" | "lower";
Mara-Li marked this conversation as resolved.
Show resolved Hide resolved
await this.plugin.saveSettings();
});
});
Mara-Li marked this conversation as resolved.
Show resolved Hide resolved
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
Loading