Skip to content

Commit

Permalink
fix: relative links and image not converted with wiki to md
Browse files Browse the repository at this point in the history
close #8
  • Loading branch information
Mara-Li committed Jun 14, 2022
1 parent 8a13340 commit 0755d37
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 57 deletions.
40 changes: 23 additions & 17 deletions mkdocsPublisher/githubInteraction/getFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand All @@ -46,38 +44,46 @@ 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 = [];
const shareKey = this.settings.shareKey;
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);
}
}
}
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;
Expand Down
11 changes: 4 additions & 7 deletions mkdocsPublisher/githubInteraction/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { Base64 } from "js-base64";
import {deleteFromGithub} from "./delete"
import {
convertLinkCitation,
convertWikilinks,
convertWikilinks, getImageLinkOptions,
getReceiptFolder
} from "../utils/utils";

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}

Expand Down
116 changes: 83 additions & 33 deletions mkdocsPublisher/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,59 +41,109 @@ 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);
}
}
}
}
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
}

0 comments on commit 0755d37

Please sign in to comment.