From 8fa583f27a28876485f124c4c9d46dab1aa6de0a Mon Sep 17 00:00:00 2001 From: Mara Date: Thu, 8 Sep 2022 01:42:33 +0200 Subject: [PATCH] fix: links creation with false & non existant files --- plugin/contents_conversion/convertLinks.ts | 23 +++++++++++----------- plugin/publishing/filesManagement.ts | 11 ++++++++--- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/plugin/contents_conversion/convertLinks.ts b/plugin/contents_conversion/convertLinks.ts index 67c1c3a2..cbece314 100644 --- a/plugin/contents_conversion/convertLinks.ts +++ b/plugin/contents_conversion/convertLinks.ts @@ -14,7 +14,7 @@ export function convertWikilinks( const convertWikilink: boolean = frontmatter.mdlinks !== undefined ? frontmatter?.mdlinks : settings.convertWikiLinks; const imageSettings: boolean = frontmatter.image !== undefined ? frontmatter?.image : settings.embedImage; const embedSettings: boolean = frontmatter.embed !== undefined ? frontmatter?.embed : settings.embedNotes; - if (!convertWikilink && frontmatter?.links && imageSettings) { + if (!convertWikilink && frontmatter?.links && imageSettings && embedSettings) { return fileContent; } const wikiRegex = /!?\[\[.*?\]\]/g; @@ -34,10 +34,11 @@ export function convertWikilinks( if (convertWikilink) { linkCreator = `${isEmbed}[${altText}](${encodeURI(linkedFile.linkFrom)})`; } - else if (frontmatter?.links === false) { + else if (linkedFile.linked.extension === 'md' && (frontmatter?.links === false || (isEmbed === '!' && !embedSettings))) { linkCreator = altText; } - else if (!imageSettings && (linkedFile.linked.extension.match('png|jpg|jpeg|gif|svg'))) { + else if (!imageSettings && (linkedFile.linked.extension.match('png|jpg|jpeg|gif|svg'))) + { linkCreator = ''; } fileContent = fileContent.replace(wikiMatch, linkCreator); @@ -45,18 +46,17 @@ export function convertWikilinks( const altMatch = wikiMatch.match(/(\|).*(]])/); const altCreator = fileName.split('/'); - const altLink = creatorAltLink(altMatch, altCreator, fileName.split('.').at(-1)); + const altLink = creatorAltLink(altMatch, altCreator, fileName.split('.').at(-1), fileName); + if (convertWikilink){ linkCreator = `${isEmbed}[${altLink}](${encodeURI(fileName.trim())})`; } - if (!embedSettings && isEmbed === '!') { - linkCreator = ''; - } - else if (frontmatter?.links === false && fileName.trim().match('md$')) { + else if (frontmatter?.links === false || (isEmbed == '!' && !embedSettings)) { linkCreator = altLink; } else if ( !imageSettings - && fileName.trim().match('(png|jpg|jpeg|gif|svg)$')) { + && fileName.trim().match('(png|jpg|jpeg|gif|svg)$')) + { linkCreator = ''; } @@ -109,7 +109,8 @@ export function convertLinkCitation( export function creatorAltLink( altMatch: RegExpMatchArray, altCreator: string[], - fileExtension: string):string { + fileExtension: string, + match: string):string { /* * Create the alt text for the link * if no alt text is given, the alt text is the filename without the extension @@ -120,5 +121,5 @@ export function creatorAltLink( if (fileExtension === 'md') { return altCreator.length > 1 ? altCreator[altCreator.length-1] : altCreator[0] //alt text based on filename for markdown files } - return '' + return match.split('/').at(-1); //alt text based on filename for other files } diff --git a/plugin/publishing/filesManagement.ts b/plugin/publishing/filesManagement.ts index 2c731deb..ff8a99e0 100644 --- a/plugin/publishing/filesManagement.ts +++ b/plugin/publishing/filesManagement.ts @@ -158,6 +158,7 @@ export class FilesManagement extends Publisher { const embedCaches = this.metadataCache.getCache(file.path).embeds; const frontmatterSourceFile = this.metadataCache.getFileCache(file).frontmatter; const imageList:TFile[] = []; + if (embedCaches != undefined) { for (const embed of embedCaches) { try { @@ -165,6 +166,7 @@ export class FilesManagement extends Publisher { embed.link, file.path ); + imageList.push(this.imageSharedOrNote(imageLink, frontmatterSourceFile)); } catch (e) { noticeLog(e, this.settings) @@ -270,12 +272,15 @@ export class FilesManagement extends Publisher { private imageSharedOrNote(file: TFile, frontmatterSourceFile: FrontMatterCache) { const transferImage = frontmatterSourceFile.image !== undefined ? frontmatterSourceFile.image : this.settings.embedImage; - if (file.extension.match(/(png|jpe?g|gif|bmp|svg|mp[34]|webm|wav|m4a|ogg|3gp|flac|ogv|mov|mkv|pdf)/i) - && transferImage + const transferEmbeds = frontmatterSourceFile.embed !== undefined ? frontmatterSourceFile.embed : this.settings.embedNotes; + if ( + (file.extension.match(/(png|jpe?g|gif|bmp|svg|mp[34]|webm|wav|m4a|ogg|3gp|flac|ogv|mov|mkv|pdf)/i) + && transferImage) + || (transferEmbeds && file.extension === 'md') ) { return file } - return file + return null } async getMetadataLinks(file: TFile, embedFiles: TFile[]) {