From 0755d375e672ceeab1a0216e0c9f4bf63ec762f7 Mon Sep 17 00:00:00 2001 From: Mara Date: Tue, 14 Jun 2022 22:26:07 +0200 Subject: [PATCH] fix: relative links and image not converted with wiki to md close #8 --- mkdocsPublisher/githubInteraction/getFiles.ts | 40 +++--- mkdocsPublisher/githubInteraction/upload.ts | 11 +- mkdocsPublisher/utils/utils.ts | 116 +++++++++++++----- 3 files changed, 110 insertions(+), 57 deletions(-) diff --git a/mkdocsPublisher/githubInteraction/getFiles.ts b/mkdocsPublisher/githubInteraction/getFiles.ts index 3f5c0b82..d22e20b2 100644 --- a/mkdocsPublisher/githubInteraction/getFiles.ts +++ b/mkdocsPublisher/githubInteraction/getFiles.ts @@ -7,7 +7,7 @@ import { } from "obsidian"; import { MkdocsPublicationSettings } from "../settings/interface"; import { Octokit } from "@octokit/core"; -import {getReceiptFolder} from "../utils/utils"; +import {getReceiptFolder, getImageLinkOptions} from "../utils/utils"; export class GetFiles { vault: Vault; @@ -33,9 +33,7 @@ export class GetFiles { const sharedkey = this.settings.shareKey; for (const file of files) { try { - const frontMatter = this.metadataCache.getCache( - file.path - ).frontmatter; + const frontMatter = this.metadataCache.getCache(file.path).frontmatter; if (frontMatter && frontMatter[sharedkey] === true) { shared_File.push(file); } @@ -46,17 +44,6 @@ export class GetFiles { return shared_File; } - createDefaultImagePath(file: TFile) { - let fileDefaultPath = file.path; - const fileName = file.name; - if (this.settings.defaultImageFolder.length > 0) { - fileDefaultPath = this.settings.defaultImageFolder + "/" + fileName; - } else if (this.settings.folderDefaultName.length > 0) { - fileDefaultPath = this.settings.folderDefaultName + "/" + fileName; - } - return fileDefaultPath; - } - getAllFileWithPath() { const files = this.vault.getFiles(); const allFileWithPath = []; @@ -64,13 +51,13 @@ export class GetFiles { for (const file of files) { const fileExtension = file.extension; if (fileExtension.match(/(png|jpe?g|svg|bmp|gif)$/i)) { - const filepath = this.createDefaultImagePath(file); + const filepath = getImageLinkOptions(file, this.settings); allFileWithPath.push(filepath); } else if (file.extension == "md") { const frontMatter = this.metadataCache.getCache( file.path ).frontmatter; - if (frontMatter && frontMatter[shareKey] === true) { + if (frontMatter && frontMatter[shareKey] === true && file.extension === "md") { const filepath = getReceiptFolder(file, this.settings, this.metadataCache); allFileWithPath.push(filepath); } @@ -78,6 +65,25 @@ export class GetFiles { } return allFileWithPath; } + + getLinkedImageAndFiles(file: TFile) { + const linkedFiles = this.getLinkedFiles(file); + const imageEmbedded = this.metadataCache.getFileCache(file).embeds; + if (imageEmbedded != undefined){ + for (const image of imageEmbedded) { + const imageLink = this.metadataCache.getFirstLinkpathDest(image.link, file.path) + const imageExt = imageLink.extension; + if (imageExt.match(/(png|jpe?g|svg|bmp|gif)$/i)) { + linkedFiles.push({ + 'linked': imageLink, + 'linkFrom' : image.link, + 'altText' : image.displayText + }) + } + } + } + return linkedFiles; + } getLinkedFiles(file: TFile): {linked: TFile, linkFrom: string, altText: string}[] { const embedCaches = this.metadataCache.getCache(file.path).links; diff --git a/mkdocsPublisher/githubInteraction/upload.ts b/mkdocsPublisher/githubInteraction/upload.ts index 49d896ae..e2b2b613 100644 --- a/mkdocsPublisher/githubInteraction/upload.ts +++ b/mkdocsPublisher/githubInteraction/upload.ts @@ -12,7 +12,7 @@ import { Base64 } from "js-base64"; import {deleteFromGithub} from "./delete" import { convertLinkCitation, - convertWikilinks, + convertWikilinks, getImageLinkOptions, getReceiptFolder } from "../utils/utils"; @@ -49,8 +49,8 @@ export default class MkdocsPublish { try { let text = await this.vault.cachedRead(file); const linkedImage = shareFiles.getLinkedImage(file); - const linkedFiles = shareFiles.getLinkedFiles(file); - text = convertLinkCitation(text, this.settings, linkedFiles, this.metadataCache) + const linkedFiles = shareFiles.getLinkedImageAndFiles(file); + text = convertLinkCitation(text, this.settings, linkedFiles, this.metadataCache, file) text = convertWikilinks(text, this.settings, linkedFiles); const path = getReceiptFolder(file, this.settings, this.metadataCache) await this.uploadText(file.path, text, path, file.name, ref); @@ -140,10 +140,7 @@ export default class MkdocsPublish { async uploadImage(imageFile: TFile, ref = "main") { const imageBin = await this.vault.readBinary(imageFile); const image64 = arrayBufferToBase64(imageBin); - let path = this.settings.folderDefaultName + "/" + imageFile.name; - if (this.settings.defaultImageFolder.length > 0) { - path = this.settings.defaultImageFolder + "/" + imageFile.name; - } + const path = getImageLinkOptions(imageFile, this.settings); await this.upload(imageFile.path, image64, path, "", ref); } diff --git a/mkdocsPublisher/utils/utils.ts b/mkdocsPublisher/utils/utils.ts index 81dfdd85..4459891d 100644 --- a/mkdocsPublisher/utils/utils.ts +++ b/mkdocsPublisher/utils/utils.ts @@ -41,19 +41,18 @@ function convertWikilinks(fileContent: string, settings: MkdocsPublicationSettin for (const wikiMatch of wikiMatches) { const fileMatch = wikiMatch.match(fileRegex); if (fileMatch) { - const fileName = fileMatch[0] - const linked=linkedFiles.find(item => item.linkFrom===fileName) - if (linked) { - const altText = linked.altText.length > 0 ? linked.altText : linked.linked.basename - const linkCreator = `[${altText}](${encodeURI(linked.linkFrom)})` - fileContent = fileContent.replace(wikiMatch, linkCreator) - } else if (!fileMatch[0].startsWith('http')) { - const altRegex = /(?<=\|).*(?=]])/; - const altMatch = wikiMatch.match(altRegex); + const fileName = fileMatch[0]; + const linkedFile=linkedFiles.find(item => item.linkFrom===fileName); + if (linkedFile) { + const altText = linkedFile.altText.length > 0 ? linkedFile.altText : linkedFile.linked.extension === 'md' ? linkedFile.linked.basename : ""; + const linkCreator = `[${altText}](${encodeURI(linkedFile.linkFrom)})`; + fileContent = fileContent.replace(wikiMatch, linkCreator); + } else if (!fileName.startsWith('http')) { + const altMatch = wikiMatch.match(/(?<=\|).*(?=]])/); const altCreator = fileName.split('/'); - const altLink = altMatch ? altMatch[0] : altCreator.length > 1 ? altCreator[altCreator.length-2] : altCreator[0]; - const linkCreator = `[${altLink}](${encodeURI(fileName.trim())})` - fileContent = fileContent.replace(wikiMatch, linkCreator) + const altLink = creatorAltLink(altMatch, altCreator, fileName.split('.').at(-1)); + const linkCreator = `[${altLink}](${encodeURI(fileName.trim())})`; + fileContent = fileContent.replace(wikiMatch, linkCreator); } } } @@ -61,39 +60,90 @@ function convertWikilinks(fileContent: string, settings: MkdocsPublicationSettin return fileContent; } -function getReceiptFolder(file: TFile, settings:MkdocsPublicationSettings, metadataCache: MetadataCache) { - const folderDefault = settings.folderDefaultName; - let path = settings.folderDefaultName.length > 0 ? settings.folderDefaultName + "/" + file.name : file.name; +function creatorAltLink(altMatch: RegExpMatchArray, altCreator: string[], fileExtension: string) { + if (altMatch) { + return altMatch[0] + } + if (fileExtension === 'md') { + return altCreator.length > 1 ? altCreator[altCreator.length-1] : altCreator[0] //alt text based on filename for markdown files + } + return '' + +} - if (settings.downloadedFolder === "yamlFrontmatter") { - const frontmatter = metadataCache.getCache(file.path).frontmatter - let folderRoot = settings.rootFolder; - if (folderRoot.length > 0) { - folderRoot = folderRoot + "/"; +function createRelativePath(sourcePath: string, targetPath: string) { + const sourceList = sourcePath.split('/'); + const targetList = targetPath.split('/'); + const diffSourcePath = sourceList.filter(x => !targetList.includes(x)); + const diffTargetPath = targetList.filter(x => !sourceList.includes(x)); + const diffTarget = function (folderPath: string[]) { + const relativePath = []; + for (const folder of folderPath) { + if (folder != folderPath.at(-1)) { + relativePath.push('..'); + } } - 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; + return relativePath; + }; + return diffTarget(diffSourcePath).concat(diffTargetPath).join('/') +} + +function getReceiptFolder(file: TFile, settings:MkdocsPublicationSettings, metadataCache: MetadataCache) { + if (file.extension === 'md') { + const folderDefault = settings.folderDefaultName; + let path = settings.folderDefaultName.length > 0 ? settings.folderDefaultName + "/" + file.name : file.name; + + if (settings.downloadedFolder === "yamlFrontmatter") { + const frontmatter = 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); } - } 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 } - return path } -function convertLinkCitation(fileContent: string, settings: MkdocsPublicationSettings, linkedFiles : {linked: TFile, linkFrom: string, altText: string}[], metadataCache: MetadataCache) { +function convertLinkCitation(fileContent: string, settings: MkdocsPublicationSettings, linkedFiles : {linked: TFile, linkFrom: string, altText: string}[], metadataCache: MetadataCache, sourceFile: TFile) { if (!settings.convertForGithub) { return fileContent; } for (const linkedFile of linkedFiles) { - const pathInGithub = getReceiptFolder(linkedFile.linked, settings, metadataCache ) + let pathInGithub=linkedFile.linked.extension === 'md' ? getReceiptFolder(linkedFile.linked, settings, metadataCache) : getImageLinkOptions(linkedFile.linked, settings); + const sourcePath = getReceiptFolder(sourceFile, settings, metadataCache); + pathInGithub = createRelativePath(sourcePath, pathInGithub); fileContent = fileContent.replace(linkedFile.linkFrom, pathInGithub) } return fileContent; } -export {disablePublish, noticeMessage, convertWikilinks, convertLinkCitation, getReceiptFolder } +function getImageLinkOptions(file: TFile, settings: MkdocsPublicationSettings) { + let fileDefaultPath = file.path; + const fileName = file.name; + if (settings.defaultImageFolder.length > 0) { + fileDefaultPath = settings.defaultImageFolder + "/" + fileName; + } else if (settings.folderDefaultName.length > 0) { + fileDefaultPath = settings.folderDefaultName + "/" + fileName; + } + return fileDefaultPath; +} + +export { + disablePublish, + noticeMessage, + convertWikilinks, + convertLinkCitation, + getReceiptFolder, + getImageLinkOptions, + createRelativePath +}