Skip to content

Commit

Permalink
feat: Adding internal links converter and wikilinks to mdlinks
Browse files Browse the repository at this point in the history
  • Loading branch information
Mara-Li committed Jun 5, 2022
1 parent 3481288 commit 742077a
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 27 deletions.
35 changes: 10 additions & 25 deletions mkdocsPublisher/githubInteraction/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ import { GetFiles } from "./getFiles";
import { Octokit } from "@octokit/core";
import { Base64 } from "js-base64";
import {deleteFromGithub} from "./delete"
import {
convertLinkCitation,
convertWikilinks,
getReceiptFolder
} from "../utils/utils";

export default class MkdocsPublish {
vault: Vault;
Expand Down Expand Up @@ -42,32 +47,12 @@ export default class MkdocsPublish {
return false;
}
try {
const text = await this.vault.cachedRead(file);
let text = await this.vault.cachedRead(file);
const linkedImage = shareFiles.getLinkedImage(file);
let folderDefault = this.settings.folderDefaultName;
if (folderDefault.length > 0) {
folderDefault = folderDefault + "/";
}
let path = folderDefault + file.name;
if (this.settings.downloadedFolder === "yamlFrontmatter") {
let folderRoot = this.settings.rootFolder;
if (folderRoot.length > 0) {
folderRoot = folderRoot + "/";
}
if (frontmatter[this.settings.yamlFolderKey]) {
const category = frontmatter[this.settings.yamlFolderKey]
let parentCatFolder = category.split('/').at(-1)
parentCatFolder = parentCatFolder.length === 0 ? category.split('/').at(-2) : parentCatFolder
const fileName = this.settings.folderNote && parentCatFolder === file.name ? 'index.md' : file.name
path =
folderRoot +
frontmatter[this.settings.yamlFolderKey] +
"/" + fileName;
}
} else if (this.settings.downloadedFolder === "obsidianPath") {
const fileName = file.name.replace('.md', '') === file.parent.name && this.settings.folderNote ? 'index.md' : file.name
path = folderDefault + file.path.replace(file.name, fileName);
}
const linkedFiles = shareFiles.getLinkedFiles(file);
text = convertLinkCitation(text, this.settings, linkedFiles)
text = convertWikilinks(text, this.settings, linkedFiles);
const path = getReceiptFolder(file, this.settings)
await this.uploadText(file.path, text, path, file.name, ref);
if (linkedImage.length > 0 && this.settings.transferEmbedded) {
for (const image of linkedImage) {
Expand Down
6 changes: 5 additions & 1 deletion mkdocsPublisher/settings/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export interface MkdocsPublicationSettings {
autoCleanUp: boolean;
autoCleanUpExcluded: string;
folderNote: boolean;
convertWikiLinks: boolean;
convertForGithub: boolean;
}

export const DEFAULT_SETTINGS: MkdocsPublicationSettings = {
Expand All @@ -38,5 +40,7 @@ export const DEFAULT_SETTINGS: MkdocsPublicationSettings = {
defaultImageFolder: '',
autoCleanUp: false,
autoCleanUpExcluded: '',
folderNote: false
folderNote: false,
convertWikiLinks: false,
convertForGithub: false,
}
66 changes: 65 additions & 1 deletion mkdocsPublisher/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,68 @@ async function noticeMessage(publish: MkdocsPublish, file: TFile | string, setti
}
}

export {disablePublish, noticeMessage }
function convertWikilinks(fileContent: string, settings: MkdocsPublicationSettings, linkedFiles: {linked: TFile, linkFrom: string, altText: string}[]) {
if (!settings.convertWikiLinks) {
return fileContent;
}
const wikiRegex = /\[\[.*?\]\]/g;
const wikiMatches = fileContent.match(wikiRegex);

if (wikiMatches) {
const fileRegex = /(?<=\[\[).*?(?=([\]|]))/;
for (const wikiMatch of wikiMatches) {
const fileMatch = wikiMatch.match(fileRegex);
if (fileMatch) {
const linked=linkedFiles.find(item => item.linkFrom===fileMatch[0])
if (linked) {
const linkCreator = `[${linked.altText}](${encodeURI(linked.linkFrom)})`
fileContent = fileContent.replace(wikiMatch, linkCreator)
} else if (!fileMatch[0].startsWith('http')) {
const altRegex = /(?<=\|).*(?=]])/;
const altMatch = wikiMatch.match(altRegex);
const altLink = altMatch ? altMatch[0] : '';
const linkCreator = `[${altLink}](${encodeURI(fileMatch[0].trim())})`
fileContent = fileContent.replace(wikiMatch, linkCreator)
}
}
}
}
return fileContent;
}

function getReceiptFolder(file: TFile, settings:MkdocsPublicationSettings) {
const folderDefault = settings.folderDefaultName;
let path = settings.folderDefaultName.length > 0 ? settings.folderDefaultName + "/" + file.name : file.name;

if (settings.downloadedFolder === "yamlFrontmatter") {
const frontmatter = this.metadataCache.getCache(file.path).frontmatter
let folderRoot = settings.rootFolder;
if (folderRoot.length > 0) {
folderRoot = folderRoot + "/";
}
if (frontmatter[settings.yamlFolderKey]) {
const category = frontmatter[settings.yamlFolderKey]
let parentCatFolder = category.split('/').at(-1)
parentCatFolder = parentCatFolder.length === 0 ? category.split('/').at(-2) : parentCatFolder
const fileName = settings.folderNote && parentCatFolder === file.name ? 'index.md' : file.name
path = folderRoot + frontmatter[settings.yamlFolderKey] + "/" + fileName;
}
} else if (settings.downloadedFolder === "obsidianPath") {
const fileName = file.name.replace('.md', '') === file.parent.name && settings.folderNote ? 'index.md' : file.name
path = folderDefault + '/' + file.path.replace(file.name, fileName);
}
return path
}

function convertLinkCitation(fileContent: string, settings: MkdocsPublicationSettings, linkedFiles : {linked: TFile, linkFrom: string, altText: string}[]) {
if (!settings.convertForGithub) {
return fileContent;
}
for (const linkedFile of linkedFiles) {
const pathInGithub = getReceiptFolder(linkedFile.linked, settings)
fileContent = fileContent.replace(linkedFile.linkFrom, pathInGithub)
}
return fileContent;
}

export {disablePublish, noticeMessage, convertWikilinks, convertLinkCitation, getReceiptFolder }

0 comments on commit 742077a

Please sign in to comment.