Skip to content

Commit 8c39766

Browse files
authored
Merge pull request #16 from mnaoumov/issue-15
Make encodeURI to behave the same way as Obsidian internally
2 parents b2973dc + 360fb6b commit 8c39766

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

src/converter.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -293,24 +293,34 @@ const createLink = (dest: LinkType, originalLink: string, altOrBlockRef: string,
293293
} else {
294294
altText = file ? file.basename : finalLink;
295295
}
296-
return `[${altText}](${encodeURI(finalLink)}${fileExtension})`;
296+
return `[${altText}](${customEncodeURI(finalLink)}${fileExtension})`;
297297
} else if (dest === 'wikiTransclusion') {
298298
return `[[${decodeURI(finalLink)}#${decodeURI(altOrBlockRef)}]]`;
299299
} else if (dest === 'mdTransclusion') {
300300
// --> To skip encoding ^
301301
let encodedBlockRef = altOrBlockRef;
302302
if (altOrBlockRef.startsWith('^')) {
303-
encodedBlockRef = encodeURI(encodedBlockRef.slice(1));
303+
encodedBlockRef = customEncodeURI(encodedBlockRef.slice(1));
304304
encodedBlockRef = `^${encodedBlockRef}`;
305305
} else {
306-
encodedBlockRef = encodeURI(encodedBlockRef);
306+
encodedBlockRef = customEncodeURI(encodedBlockRef);
307307
}
308-
return `[](${encodeURI(finalLink)}${fileExtension}#${encodedBlockRef})`;
308+
return `[](${customEncodeURI(finalLink)}${fileExtension}#${encodedBlockRef})`;
309309
}
310310

311311
return '';
312312
};
313313

314+
/**
315+
* Encode URI the same way Obsidian is doing it internally
316+
*
317+
* @param uri
318+
* @returns
319+
*/
320+
function customEncodeURI(uri: string): string {
321+
return uri.replace(/[\\\x00\x08\x0B\x0C\x0E-\x1F ]/g, urlPart => encodeURIComponent(urlPart));
322+
}
323+
314324
/**
315325
*
316326
* @param sourceFilePath Path of the file, in which the links are going to be used

0 commit comments

Comments
 (0)