Skip to content

Commit

Permalink
feat(embed): allow to convert embed to links
Browse files Browse the repository at this point in the history
Remove the `!`
Replace it by other char.
Default is " > " so : `![[link]]` will become `-> [[link]]`
  • Loading branch information
Mara-Li committed Aug 9, 2023
1 parent e6f8072 commit f28d6cd
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 24 deletions.
34 changes: 25 additions & 9 deletions src/conversion/links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function convertWikilinks(
const fileRegex = /(\[\[).*?([\]|])/;
for (const wikiMatch of wikiMatches) {
const fileMatch = wikiMatch.match(fileRegex);
const isEmbed = wikiMatch.startsWith("!") ? "!" : "";
let isEmbed = wikiMatch.startsWith("!") ? "!" : "";
const isEmbedBool = wikiMatch.startsWith("!");
if (fileMatch) {
// @ts-ignore
Expand Down Expand Up @@ -85,21 +85,28 @@ export function convertWikilinks(
: linkedFile.linked.basename;
}
const removeEmbed =
conditionConvert.removeEmbed &&
conditionConvert.removeEmbed === "remove" &&
isEmbedBool &&
linkedFile.linked.extension === "md";
if (isEmbedBool && linkedFile.linked.extension === "md" && conditionConvert.removeEmbed === "links") {
isEmbed = conditionConvert.charEmbedLinks + " ";
linkCreator = linkCreator.replace("!", isEmbed);
}
if (convertWikilink) {
const altMatch = wikiMatch.match(/(\|).*(]])/);
const altCreator = fileName.split("/");
const altLink = creatorAltLink(
let altLink = creatorAltLink(
altMatch,
altCreator,
fileName.split(".").at(-1),
fileName
);
linkCreator = createMarkdownLinks(fileName, isEmbed, altLink, settings);
}

altLink = altLink
.replace("#", " > ")
.replace(/ > \^\w*/, "");
linkCreator = createMarkdownLinks(fileName, isEmbed, altLink, settings);
}
if (
linkedFile.linked.extension === "md" &&
!convertLinks &&
Expand All @@ -115,23 +122,31 @@ export function convertWikilinks(
linkCreator = "";
}
fileContent = fileContent.replace(wikiMatch, linkCreator);

} else if (!fileName.startsWith("http")) {
const altMatch = wikiMatch.match(/(\|).*(]])/);
const altCreator = fileName.split("/");

const altLink = creatorAltLink(
let altLink = creatorAltLink(
altMatch,
altCreator,
fileName.split(".").at(-1),
fileName
);
altLink = altLink
.replace("#", " > ")
.replace(/ > \^\w*/, "");
const removeEmbed =
!isAttachment(fileName.trim()) &&
conditionConvert.removeEmbed &&
conditionConvert.removeEmbed === "remove" &&
isEmbedBool;
if (isEmbedBool && conditionConvert.removeEmbed === "links" && !isAttachment(fileName.trim())) {
isEmbed = conditionConvert.charEmbedLinks + " ";
linkCreator = linkCreator.replace("!", isEmbed);
}
if (convertWikilink) {
linkCreator = createMarkdownLinks(fileName, isEmbed, altLink, settings);
}
}
if (
!isAttachment(fileName.trim()) &&
!convertLinks &&
Expand All @@ -145,7 +160,6 @@ export function convertWikilinks(
) {
linkCreator = "";
}

fileContent = fileContent.replace(wikiMatch, linkCreator);
}
}
Expand All @@ -155,6 +169,8 @@ export function convertWikilinks(
}




function createMarkdownLinks(fileName: string, isEmbed: string, altLink: string, settings: GitHubPublisherSettings) {
const markdownName = !isAttachment(fileName.trim())
? fileName.replace(/#.*/, "").trim() + ".md"
Expand Down
15 changes: 14 additions & 1 deletion src/i18n/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@
"scanningRepo": "Scanning the repository, may take a while...",
"sendMessage": "Upload {{- nbNotes}} notes to {{- repo.owner}}/{{- repo.repo}}",
"startingClean": "Starting cleaning {{- repo.owner}}/{{- repo.repo}}",
"successfulPublish": "Successfully uploaded {{- nbNotes}} to {{- repo.owner}}/{{- repo.repo}}",
"successPublishOneNote": "Successfully uploaded {{- file}} to {{- repo.owner}}/{{- repo.repo}}",
"successfulPublish": "Successfully uploaded {{- nbNotes}} to {{- repo.owner}}/{{- repo.repo}}",
"waitingWorkflow": "Now, waiting for the workflow to be completed..."
},
"modals": {
Expand Down Expand Up @@ -201,10 +201,23 @@
"title": "Content's conversion"
},
"embed": {
"char": {
"desc": "Character(s) to use to replace the ! of the embeds",
"title": "Embed characters"
},
"defaultImageFolder": {
"desc": "To use a folder different from default",
"title": "Default attachment folder"
},
"links": {
"desc": "Allow to edit the links of the embeds, removing entirely the citation, or transform to a simple link",
"dp": {
"keep": "No change",
"links": "Transform to link",
"remove": "Remove link completely"
},
"title": "Change embed markup"
},
"title": "Embed",
"transferImage": {
"desc": "Send attachments embedded in a file to GitHub.",
Expand Down
15 changes: 14 additions & 1 deletion src/i18n/locales/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@
"scanningRepo": "Scan du dépôt, veuillez patienter...",
"sendMessage": "Transfert de {{- nbNotes}} notes vers {{- repo.owner}}/{{- repo.repo}}",
"startingClean": "Début du nettoyage de {{- repo.owner}}/{{- repo.repo}}",
"successfulPublish": "Transfert réussi de {{- nbNotes}} notes vers {{- repo.owner}}/{{- repo.repo}}",
"successPublishOneNote": "Transfert réussi de {{- file}} vers {{- repo.owner}}/{{- repo.repo}}",
"successfulPublish": "Transfert réussi de {{- nbNotes}} notes vers {{- repo.owner}}/{{- repo.repo}}",
"waitingWorkflow": "Maintenant, attente de la fin du workflow..."
},
"modals": {
Expand Down Expand Up @@ -201,10 +201,23 @@
"title": "Conversion du contenu"
},
"embed": {
"char": {
"desc": "Caractère à utiliser pour remplacer le ! des embeds",
"title": "Caractère d'embed"
},
"defaultImageFolder": {
"desc": "Pour utiliser un dossier différent de celui par défaut pour les pièces-jointes.",
"title": "Dossier de pièces-jointes par défaut"
},
"links": {
"desc": "Permet d'éditer les liens des embeds, en supprimant entièrement la citation, ou en la transformant en un simple lien.",
"dp": {
"keep": "Pas de changement",
"links": "Transformer en lien simple",
"remove": "Supprimer le lien complètement"
},
"title": "Changer les balises d'embed"
},
"title": "Embed",
"transferImage": {
"desc": "Transférer les pièces-jointes",
Expand Down
49 changes: 41 additions & 8 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
supportMe,
usefullLinks} from "./settings/help";
import {
enumbSettingsTabId, FolderSettings, GithubTiersVersion, Repository } from "./settings/interface";
EnumbSettingsTabId, FolderSettings, GithubTiersVersion, Repository } from "./settings/interface";
import { migrateToken } from "./settings/migrate";
import {ExportModal, ImportLoadPreset, ImportModal, loadAllPresets} from "./settings/modals/import_export";
import {ModalAddingNewRepository} from "./settings/modals/manage_repo";
Expand Down Expand Up @@ -176,7 +176,7 @@ export class GithubPublisherSettingsTab extends PluginSettingTab {
.onChange(async (value) => {
githubSettings.api.tiersForApi = value as GithubTiersVersion;
await this.plugin.saveSettings();
this.renderSettingsPage(enumbSettingsTabId.github);
this.renderSettingsPage(EnumbSettingsTabId.github);
});
});
if (githubSettings.api.tiersForApi === GithubTiersVersion.entreprise) {
Expand Down Expand Up @@ -377,7 +377,7 @@ export class GithubPublisherSettingsTab extends PluginSettingTab {
value,
this.plugin);
await this.plugin.saveSettings();
this.renderSettingsPage(enumbSettingsTabId.upload);
this.renderSettingsPage(EnumbSettingsTabId.upload);
});
});

Expand Down Expand Up @@ -447,7 +447,7 @@ export class GithubPublisherSettingsTab extends PluginSettingTab {
.onChange(async (value) => {
uploadSettings.frontmatterTitle.enable = value;
await this.plugin.saveSettings();
this.renderSettingsPage(enumbSettingsTabId.upload);
this.renderSettingsPage(EnumbSettingsTabId.upload);
});
});
if (uploadSettings.frontmatterTitle.enable) {
Expand Down Expand Up @@ -519,7 +519,7 @@ export class GithubPublisherSettingsTab extends PluginSettingTab {


//@ts-ignore
if (app.plugins.enabledPlugins.has("metadata-extractor")) {
if (this.app.plugins.enabledPlugins.has("metadata-extractor")) {
new Setting(this.settingsPage)
.setName(
i18next.t("settings.githubWorkflow.useMetadataExtractor.title")
Expand Down Expand Up @@ -553,7 +553,7 @@ export class GithubPublisherSettingsTab extends PluginSettingTab {
.onChange(async (value) => {
uploadSettings.autoclean.enable = value;
await this.plugin.saveSettings();
this.renderSettingsPage(enumbSettingsTabId.upload);
this.renderSettingsPage(EnumbSettingsTabId.upload);
});
});
if (uploadSettings.autoclean.enable) {
Expand Down Expand Up @@ -769,6 +769,7 @@ export class GithubPublisherSettingsTab extends PluginSettingTab {
* Render the settings page for the embeds settings
*/
renderEmbedConfiguration() {
this.settingsPage.empty();
const embedSettings = this.plugin.settings.embed;
new Setting(this.settingsPage)
.setName(i18next.t("settings.embed.transferImage.title"))
Expand Down Expand Up @@ -825,10 +826,42 @@ export class GithubPublisherSettingsTab extends PluginSettingTab {
.onChange(async (value) => {
embedSettings.notes = value;
await this.plugin.saveSettings();
this.renderEmbedConfiguration();
});
});


if (embedSettings.notes) {
//@TODO translate this
new Setting(this.settingsPage)
.setName(i18next.t("settings.embed.links.title"))
.setDesc(i18next.t("settings.embed.links.desc"))
.addDropdown((dropdown) => {
dropdown
.addOption("keep", i18next.t("settings.embed.links.dp.keep"))
.addOption("remove", i18next.t("settings.embed.links.dp.remove"))
.addOption("links", i18next.t("settings.embed.links.dp.links"))
.setValue(embedSettings.convertEmbedToLinks ?? "keep")
.onChange(async (value) => {
embedSettings.convertEmbedToLinks = value as "keep" | "remove" | "links";
await this.plugin.saveSettings();
this.renderEmbedConfiguration();
});
});

if (embedSettings.convertEmbedToLinks === "links") {
new Setting(this.settingsPage)
.setName(i18next.t("settings.embed.char.title"))
.setDesc(i18next.t("settings.embed.char.desc"))
.addText((text) => {
text.setPlaceholder("->")
.setValue(embedSettings.charConvert ?? "->")
.onChange(async (value) => {
embedSettings.charConvert = value;
await this.plugin.saveSettings();
});
});
}
}
}

/**
Expand Down Expand Up @@ -895,7 +928,7 @@ export class GithubPublisherSettingsTab extends PluginSettingTab {
.onChange(async (value) => {
pluginSettings.copyLink.enable = value;
await this.plugin.saveSettings();
this.renderSettingsPage(enumbSettingsTabId.plugin);
this.renderSettingsPage(EnumbSettingsTabId.plugin);

})
);
Expand Down
9 changes: 7 additions & 2 deletions src/settings/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export enum TypeOfEditRegex {



export enum enumbSettingsTabId {
export enum EnumbSettingsTabId {
github = "github-configuration",
upload = "upload-configuration",
text = "text-conversion",
Expand Down Expand Up @@ -111,6 +111,8 @@ export interface GitHubPublisherSettings {
keySendFile: string[];
notes: boolean;
folder: string;
convertEmbedToLinks: "links" | "remove" | "keep";
charConvert: string;
}
plugin:
{
Expand Down Expand Up @@ -222,6 +224,8 @@ export const DEFAULT_SETTINGS: GitHubPublisherSettings = {
keySendFile: [],
notes: false,
folder: "",
convertEmbedToLinks: "keep",
charConvert: "->",
},
plugin: {
shareKey: "share",
Expand Down Expand Up @@ -287,7 +291,8 @@ export interface FrontmatterConvert {
embed: boolean;
attachmentLinks: string;
convertWiki: boolean;
removeEmbed: boolean;
removeEmbed: "keep" | "remove" | "links";
charEmbedLinks: string;
dataview: boolean;
hardbreak: boolean;
convertInternalNonShared: boolean;
Expand Down
2 changes: 2 additions & 0 deletions src/settings/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,8 @@ async function migrateOldSettings(plugin: GithubPublisher, old: OldSettings) {
keySendFile: old.metadataFileFields,
notes: old.embedNotes,
folder: old.defaultImageFolder,
charConvert: "->",
convertEmbedToLinks: "keep"
},
plugin: {
shareKey: old.shareKey,
Expand Down
20 changes: 17 additions & 3 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,8 @@ export function getFrontmatterCondition(
embed: settings.embed.notes,
attachmentLinks: imageDefaultFolder,
links: true,
removeEmbed: false,
removeEmbed: settings.embed.convertEmbedToLinks,
charEmbedLinks: settings.embed.charConvert,
dataview: settings.conversion.dataview,
hardbreak: settings.conversion.hardbreak,
convertInternalNonShared: settings.conversion.links.unshared,
Expand Down Expand Up @@ -342,7 +343,10 @@ export function getFrontmatterCondition(
settingsConversion.embed = frontmatter.embed.send;
}
if (frontmatter.embed.remove !== undefined) {
settingsConversion.removeEmbed = frontmatter.embed.remove;
settingsConversion.removeEmbed = translateBooleanForRemoveEmbed(frontmatter.embed.remove);
}
if (frontmatter.embed.char !== undefined) {
settingsConversion.charEmbedLinks = frontmatter.embed.char;
}
} else {
settingsConversion.embed = frontmatter.embed;
Expand Down Expand Up @@ -370,7 +374,7 @@ export function getFrontmatterCondition(
settingsConversion.convertWiki = frontmatter.mdlinks;
}
if (frontmatter.removeEmbed !== undefined) {
settingsConversion.removeEmbed = frontmatter.removeEmbed;
settingsConversion.removeEmbed = translateBooleanForRemoveEmbed(frontmatter.removeEmbed);
}
if (frontmatter.dataview !== undefined) {
settingsConversion.dataview = frontmatter.dataview;
Expand All @@ -387,6 +391,16 @@ export function getFrontmatterCondition(
return settingsConversion;
}

function translateBooleanForRemoveEmbed(removeEmbed: unknown) {
if (removeEmbed === "true") {
return "keep";
} else if (removeEmbed === "false") {
return "remove";
} else if (removeEmbed === "links") {
return "links";
} else return "keep";
}

/**
* Get the frontmatter from the frontmatter
* @param {GitHubPublisherSettings} settings
Expand Down

0 comments on commit f28d6cd

Please sign in to comment.