Skip to content

Commit 93035e1

Browse files
sijie123yamgent
authored andcommitted
Parser: Fix crash if no href given to anchor or link tag (#780)
MarkBind crashes if no href is given to an anchor or link tag, as it tries to rewrite a non-existent URL. Let's introduce an extra check for the href attribute. Only if the href attribute is present, do we proceed to rewrite URL to their absolute paths.
1 parent a159a78 commit 93035e1

File tree

4 files changed

+16
-7
lines changed

4 files changed

+16
-7
lines changed

src/lib/markbind/src/parser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ Parser.prototype._preprocess = function (node, context, config) {
387387
element.attribs.src = utils.ensurePosix(resultPath);
388388
}
389389
} else if (['a', 'link'].includes(element.name)) {
390-
if (!isUrl && !isAbsolutePath) {
390+
if (!isUrl && !isAbsolutePath && hasHref) {
391391
const resultPath = path.join('{{hostBaseUrl}}', path.relative(config.rootPath, filePath));
392392
element.attribs.href = utils.ensurePosix(resultPath);
393393
}

test/functional/test_site/expected/index.html

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -479,8 +479,11 @@ <h2 id="feature-list">Feature list<a class="fa fa-anchor" href="#feature-list"><
479479
<pic src="/test_site/sub_site/images/I'm not allowed to use my favorite tool.png"></pic>
480480
<pic src="https://dummyimage.com/600x400/000/fff"></pic>
481481
</p>
482-
<p>Anchor link:
483-
<a href="https://dummyimage.com/600x400/000/fff">External Image</a></p>
482+
<p>Anchor:
483+
<a href="https://dummyimage.com/600x400/000/fff">External Image</a>
484+
<a href="/test_site/sub_site/images/I'm not allowed to use my favorite tool.png">Link to picture</a>
485+
<a id="namedAnchor">Named Anchor</a>
486+
<a>Anchor with no attributes</a></p>
484487
</div>
485488
<h1 id="trimmed-include">Trimmed include<a class="fa fa-anchor" href="#trimmed-include"></a></h1>
486489
<h2 id="fragment-with-leading-spaces-and-newline"><span>Fragment with leading spaces and newline</span><a class="fa fa-anchor" href="#fragment-with-leading-spaces-and-newline"></a></h2>

test/functional/test_site/expected/sub_site/testReuse._include_.html

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,8 @@
1414
<pic src="/test_site/sub_site/images/I'm not allowed to use my favorite tool.png"></pic>
1515
<pic src="https://dummyimage.com/600x400/000/fff"></pic>
1616
</p>
17-
<p>Anchor link:
18-
<a href="https://dummyimage.com/600x400/000/fff">External Image</a></p>
17+
<p>Anchor:
18+
<a href="https://dummyimage.com/600x400/000/fff">External Image</a>
19+
<a href="/test_site/sub_site/images/I'm not allowed to use my favorite tool.png">Link to picture</a>
20+
<a id="namedAnchor">Named Anchor</a>
21+
<a>Anchor with no attributes</a></p>

test/functional/test_site/sub_site/testReuse.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,8 @@ PIC tags:
1919
<pic src="{{baseUrl}}/images/I'm not allowed to use my favorite tool.png"></pic>
2020
<pic src="https://dummyimage.com/600x400/000/fff"></pic>
2121

22-
Anchor link:
23-
<a href="https://dummyimage.com/600x400/000/fff">External Image</a>
22+
Anchor:
23+
<a href="https://dummyimage.com/600x400/000/fff">External Image</a>
24+
<a href="{{baseUrl}}/images/I'm not allowed to use my favorite tool.png">Link to picture</a>
25+
<a id="namedAnchor">Named Anchor</a>
26+
<a>Anchor with no attributes</a>

0 commit comments

Comments
 (0)