Skip to content

Commit 7b312f8

Browse files
author
Ozan Tellioglu
committed
Final Link Alt Text Correction
1 parent 32671bc commit 7b312f8

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

src/converter.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -234,17 +234,32 @@ const getFileLinkInFormat = (file: TFile, sourceFile: TFile, plugin: LinkConvert
234234

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

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

244243
if (dest === 'wiki') {
245-
return `[[${decodeURI(finalLink)}${altOrBlockRef !== '' && altOrBlockRef !== decodeURI(finalLink) ? '|' + altOrBlockRef : ''}]]`;
244+
// If alt text is same as the final link or same as file base name, it needs to be empty
245+
if (altOrBlockRef !== '' && altOrBlockRef !== decodeURI(finalLink)) {
246+
if (file && decodeURI(altOrBlockRef) === file.basename) {
247+
altText = '';
248+
} else {
249+
altText = '|' + altOrBlockRef;
250+
}
251+
} else {
252+
altText = '';
253+
}
254+
return `[[${decodeURI(finalLink)}${altText}]]`;
246255
} else if (dest === 'markdown') {
247-
return `[${altOrBlockRef !== '' ? altOrBlockRef : finalLink}](${encodeURI(finalLink)})`;
256+
// If there is no alt text specifiec and file exists, the alt text needs to be always the file base name
257+
if (altOrBlockRef !== '') {
258+
altText = altOrBlockRef;
259+
} else {
260+
altText = file ? file.basename : finalLink;
261+
}
262+
return `[${altText}](${encodeURI(finalLink)})`;
248263
} else if (dest === 'wikiTransclusion') {
249264
return `[[${decodeURI(finalLink)}#${decodeURI(altOrBlockRef)}]]`;
250265
} else if (dest === 'mdTransclusion') {

0 commit comments

Comments
 (0)