Skip to content

Commit

Permalink
fix: allow multiple option for slugify
Browse files Browse the repository at this point in the history
  • Loading branch information
Mara-Li committed Jan 26, 2024
1 parent a9c32e6 commit 01f541a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 21 deletions.
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") ?? "";
}

/**
* 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
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: "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();
});
});
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 01f541a

Please sign in to comment.