Skip to content

Commit

Permalink
fix: handling links that have internalHtml but no href (#555)
Browse files Browse the repository at this point in the history
  • Loading branch information
akosbalasko authored Dec 29, 2023
1 parent 72c32d8 commit e95a286
Show file tree
Hide file tree
Showing 5 changed files with 2,754 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/utils/turndown-rules/images-rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const imagesRule = {
return `![](${realValue}${sizeString})`;
} else if (yarleOptions.keepImageSize === OutputFormat.ObsidianMD) {
sizeString = (widthParam || heightParam) ? `|${widthParam || 0}x${heightParam || 0}` : '';
if (realValue.startsWith('./')) {
if (realValue.startsWith('./') || realValue.startsWith('..')) {
return `![[${realValue}${sizeString}]]`;
} else {
return `![${sizeString}](${realValue})`;
Expand Down
12 changes: 7 additions & 5 deletions src/utils/turndown-rules/internal-links-rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ export const wikiStyleLinksRule = {
filter: filterByNodeName('A'),
replacement: (content: any, node: any) => {
const nodeProxy = getAttributeProxy(node);

if (!nodeProxy.href) {
return '';
}
let internalTurndownedContent =
getTurndownService(yarleOptions).turndown(removeBrackets(node.innerHTML));
internalTurndownedContent = removeDoubleBackSlashes(internalTurndownedContent);
if (!nodeProxy.href) {
return (node.innerHTML === '' || !node.innerHTML)
? ''
: internalTurndownedContent;
}

const lexer = new marked.Lexer({});
const tokens = lexer.lex(internalTurndownedContent) as any;
const extension = yarleOptions.addExtensionToInternalLinks ? '.md' : '';
Expand All @@ -50,7 +52,7 @@ export const wikiStyleLinksRule = {
token = tokens[0];
token['mdKeyword'] = `${'#'.repeat(tokens[0]['depth'])} `;
}
const value = nodeProxy.href.value;
const value = nodeProxy.href ? nodeProxy.href.value : internalTurndownedContent;
const type = nodeProxy.type ? nodeProxy.type.value : undefined ;
const realValue = yarleOptions.urlEncodeFileNamesAndLinks ? encodeURI(value) : value;

Expand Down
Loading

0 comments on commit e95a286

Please sign in to comment.