Skip to content

Commit

Permalink
Final Link Alt Text Correction
Browse files Browse the repository at this point in the history
  • Loading branch information
Ozan Tellioglu committed Oct 23, 2021
1 parent 32671bc commit 7b312f8
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions src/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,17 +234,32 @@ const getFileLinkInFormat = (file: TFile, sourceFile: TFile, plugin: LinkConvert

const createLink = (dest: LinkType, originalLink: string, altOrBlockRef: string, sourceFile: TFile, plugin: LinkConverterPlugin): string => {
let finalLink = originalLink;
let altText: string;

if (plugin.settings.finalLinkFormat !== 'not-change') {
let fileLink = decodeURI(finalLink);
let file = plugin.app.metadataCache.getFirstLinkpathDest(fileLink, sourceFile.path);
if (file) finalLink = getFileLinkInFormat(file, sourceFile, plugin, plugin.settings.finalLinkFormat);
}
let fileLink = decodeURI(finalLink);
let file = plugin.app.metadataCache.getFirstLinkpathDest(fileLink, sourceFile.path);
if (file && plugin.settings.finalLinkFormat !== 'not-change') finalLink = getFileLinkInFormat(file, sourceFile, plugin, plugin.settings.finalLinkFormat);

if (dest === 'wiki') {
return `[[${decodeURI(finalLink)}${altOrBlockRef !== '' && altOrBlockRef !== decodeURI(finalLink) ? '|' + altOrBlockRef : ''}]]`;
// If alt text is same as the final link or same as file base name, it needs to be empty
if (altOrBlockRef !== '' && altOrBlockRef !== decodeURI(finalLink)) {
if (file && decodeURI(altOrBlockRef) === file.basename) {
altText = '';
} else {
altText = '|' + altOrBlockRef;
}
} else {
altText = '';
}
return `[[${decodeURI(finalLink)}${altText}]]`;
} else if (dest === 'markdown') {
return `[${altOrBlockRef !== '' ? altOrBlockRef : finalLink}](${encodeURI(finalLink)})`;
// If there is no alt text specifiec and file exists, the alt text needs to be always the file base name
if (altOrBlockRef !== '') {
altText = altOrBlockRef;
} else {
altText = file ? file.basename : finalLink;
}
return `[${altText}](${encodeURI(finalLink)})`;
} else if (dest === 'wikiTransclusion') {
return `[[${decodeURI(finalLink)}#${decodeURI(altOrBlockRef)}]]`;
} else if (dest === 'mdTransclusion') {
Expand Down

0 comments on commit 7b312f8

Please sign in to comment.