Skip to content

Commit

Permalink
use raw-data-href if available in img tags
Browse files Browse the repository at this point in the history
  • Loading branch information
robertKozik committed Apr 8, 2024
1 parent 9ffadbb commit f6a1970
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
13 changes: 13 additions & 0 deletions parser/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -502,4 +502,17 @@ describe('inline image', () => {
{type: 'syntax', start: 31, length: 1},
]);
});

test('image with empty alt text and not completed link', () => {
expect('# ![](onet.pl)').toBeParsedAs([
{type: 'syntax', start: 0, length: 2},
{type: 'h1', start: 2, length: 12},
{type: 'syntax', start: 2, length: 1},
{type: 'syntax', start: 3, length: 1},
{type: 'syntax', start: 4, length: 1},
{type: 'syntax', start: 5, length: 1},
{type: 'link', start: 6, length: 7},
{type: 'syntax', start: 13, length: 1},
]);
});
});
4 changes: 3 additions & 1 deletion parser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ function parseTreeToTextAndRanges(tree: StackItem): [string, Range[]] {
const src = node.tag.match(/src="([^"]*)"/)![1]!; // always present
const alt = node.tag.match(/alt="([^"]*)"/);
const hasAlt = node.tag.match(/data-link-variant="([^"]*)"/)![1] === 'labeled';
const rawLink = node.tag.match(/data-raw-href="([^"]*)"/);
const linkString = rawLink ? _.unescape(rawLink[1]!) : src;

appendSyntax('!');
if (hasAlt) {
Expand All @@ -174,7 +176,7 @@ function parseTreeToTextAndRanges(tree: StackItem): [string, Range[]] {
appendSyntax(']');
}
appendSyntax('(');
addChildrenWithStyle(src, 'link');
addChildrenWithStyle(linkString, 'link');
appendSyntax(')');
} else {
throw new Error(`Unknown tag: ${node.tag}`);
Expand Down
Loading

0 comments on commit f6a1970

Please sign in to comment.