Skip to content

Commit

Permalink
fix: Fix rehype-remark replace all & in the source to \& caus…
Browse files Browse the repository at this point in the history
…e mismatch with real url

rehypejs/rehype-remark#14
  • Loading branch information
linonetwo committed Jan 10, 2024
1 parent 0b4dc4c commit cc69c07
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/popup/hooks/useContentToSave.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,22 @@ function replaceAnAsset(noteTitle: string, content: string, selectedContentKey:
}
}

/**
* Fix rehype-remark replace all `&` in the source to `\&`, but we still can get real url from `asset.url`
* @param url image url that might contains some special chars
* @returns escape special chars
* @url https://github.com/rehypejs/rehype-remark/issues/14
*/
function fixUrlEscape(url: string): string {
return url.replaceAll('&', '\\&');
}

function replaceAnImageInMarkdown(noteTitle: string, content: string, asset: Asset): string {
const stringToReplace = `![${asset.alt ?? ''}](${asset.url})`;
const stringToReplace = `![${asset.alt ?? ''}](${fixUrlEscape(asset.url)})`;
return content.replaceAll(stringToReplace, `![${asset.alt ?? ''}](${asset.title})`);
}
function replaceAnImageInWikitext(noteTitle: string, content: string, asset: Asset): string {
const stringToReplace = `[img[${asset.url}]]`;
const stringToReplace = `[img[${fixUrlEscape(asset.url)}]]`;
const alt = asset.alt ? `${asset.alt}|` : '';
return content.replaceAll(stringToReplace, `[img[${alt}${asset.title}]]`);
}
Expand Down

0 comments on commit cc69c07

Please sign in to comment.