Skip to content

Commit

Permalink
fix: links creation with false & non existant files
Browse files Browse the repository at this point in the history
  • Loading branch information
Mara-Li committed Sep 7, 2022
1 parent ee63c04 commit 8fa583f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
23 changes: 12 additions & 11 deletions plugin/contents_conversion/convertLinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -34,29 +34,29 @@ 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);
} else if (!fileName.startsWith('http')) {
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 = '';
}

Expand Down Expand Up @@ -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
Expand All @@ -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
}
11 changes: 8 additions & 3 deletions plugin/publishing/filesManagement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,15 @@ 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 {
const imageLink = this.metadataCache.getFirstLinkpathDest(
embed.link,
file.path
);

imageList.push(this.imageSharedOrNote(imageLink, frontmatterSourceFile));
} catch (e) {
noticeLog(e, this.settings)
Expand Down Expand Up @@ -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[]) {
Expand Down

0 comments on commit 8fa583f

Please sign in to comment.