Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions src/Renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,18 +157,22 @@ export class _Renderer<ParserOutput = string, RendererOutput = string> {
return `<del>${this.parser.parseInline(tokens)}</del>` as RendererOutput;
}

link({ href, title, tokens }: Tokens.Link): RendererOutput {
const text = this.parser.parseInline(tokens) as string;
link({ href, title, text, tokens, autolink }: Tokens.Link): RendererOutput {
// References are not resolved inside an autolink, so every `&` there is
// literal. Elsewhere only an `&` that cannot start one needs escaping.
const parsedText = autolink
? escapeHtmlEntities(text, true)
: this.parser.parseInline(tokens) as string;
const cleanHref = cleanUrl(href);
if (cleanHref === null) {
return text as RendererOutput;
return parsedText as RendererOutput;
}
href = cleanHref;
href = escapeHtmlEntities(cleanHref, autolink);
let out = '<a href="' + href + '"';
if (title) {
out += ' title="' + (escapeHtmlEntities(title)) + '"';
}
out += '>' + text + '</a>';
out += '>' + parsedText + '</a>';
return out as RendererOutput;
}

Expand All @@ -182,7 +186,7 @@ export class _Renderer<ParserOutput = string, RendererOutput = string> {
}
href = cleanHref;

let out = `<img src="${href}" alt="${escapeHtmlEntities(text)}"`;
let out = `<img src="${escapeHtmlEntities(href)}" alt="${escapeHtmlEntities(text)}"`;
if (title) {
out += ` title="${escapeHtmlEntities(title)}"`;
}
Expand Down
2 changes: 2 additions & 0 deletions src/Tokenizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,7 @@ export class _Tokenizer<ParserOutput = string, RendererOutput = string> {
raw: cap[0],
text,
href,
autolink: true,
tokens: [
{
type: 'text',
Expand Down Expand Up @@ -951,6 +952,7 @@ export class _Tokenizer<ParserOutput = string, RendererOutput = string> {
raw: cap[0],
text,
href,
autolink: true,
tokens: [
{
type: 'text',
Expand Down
5 changes: 5 additions & 0 deletions src/Tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ export namespace Tokens {
title?: string | null;
text: string;
tokens: Token[];
/**
* Set for autolinks and extended (GFM) urls, where character references are
* not resolved, so the destination and text are literal.
*/
autolink?: boolean;
}

export interface List {
Expand Down
15 changes: 15 additions & 0 deletions test/specs/new/link_destination_character_references.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<p><a href="https://example.com/?x=1&amp;lt;2">https://example.com/?x=1&amp;lt;2</a></p>
<p><a href="https://example.com/?y=1&amp;amp;2">https://example.com/?y=1&amp;amp;2</a></p>
<p><a href="https://example.com/?a=1&amp;b=2">https://example.com/?a=1&amp;b=2</a></p>
<p><a href="https://example.com/?x=1&amp;lt;2">https://example.com/?x=1&amp;lt;2</a></p>
<p><a href="https://example.com/?y=1&amp;amp;2">https://example.com/?y=1&amp;amp;2</a></p>
<p><a href="https://example.com/?a=1&amp;b=2">https://example.com/?a=1&amp;b=2</a></p>
<p><a href="https://example.com/?x=1&lt;2">https://example.com/?x=1&lt;2</a></p>
<p><a href="https://example.com/?y=1&amp;2">https://example.com/?y=1&amp;2</a></p>
<p><a href="https://example.com/?a=1&amp;b=2">https://example.com/?a=1&amp;b=2</a></p>
<p><a href="https://example.com/?x=1&lt;2">https://example.com/?x=1&lt;2</a></p>
<p><a href="https://example.com/?y=1&amp;2">https://example.com/?y=1&amp;2</a></p>
<p><a href="https://example.com/?a=1&amp;b=2">https://example.com/?a=1&amp;b=2</a></p>
<p><img src="https://example.com/?x=1&lt;2" alt="https://example.com/?x=1&lt;2"></p>
<p><img src="https://example.com/?y=1&amp;2" alt="https://example.com/?y=1&amp;2"></p>
<p><img src="https://example.com/?a=1&amp;b=2" alt="https://example.com/?a=1&amp;b=2"></p>
45 changes: 45 additions & 0 deletions test/specs/new/link_destination_character_references.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
# Character references resolve in a link destination but not in an autolink, so
# the same query string has to be escaped differently depending on where it is
# written.
#
# The `&lt;` inline links, reference links and images below do not match
# CommonMark. It resolves the reference and percent-encodes the result, giving
# `?x=1%3C2`, where marked keeps `?x=1&lt;2`. That difference comes from URL
# encoding rather than from this escaping, and it is the same on `master`.
# Everything else here matches CommonMark.
renderExact: true
---
https://example.com/?x=1&lt;2

https://example.com/?y=1&amp;2

https://example.com/?a=1&b=2

<https://example.com/?x=1&lt;2>

<https://example.com/?y=1&amp;2>

<https://example.com/?a=1&b=2>

[https://example.com/?x=1&lt;2](https://example.com/?x=1&lt;2)

[https://example.com/?y=1&amp;2](https://example.com/?y=1&amp;2)

[https://example.com/?a=1&b=2](https://example.com/?a=1&b=2)

[https://example.com/?x=1&lt;2][link1]

[https://example.com/?y=1&amp;2][link2]

[https://example.com/?a=1&b=2][link3]

![https://example.com/?x=1&lt;2](https://example.com/?x=1&lt;2)

![https://example.com/?y=1&amp;2](https://example.com/?y=1&amp;2)

![https://example.com/?a=1&b=2](https://example.com/?a=1&b=2)

[link1]: https://example.com/?x=1&lt;2
[link2]: https://example.com/?y=1&amp;2
[link3]: https://example.com/?a=1&b=2
4 changes: 4 additions & 0 deletions test/unit/Lexer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2042,6 +2042,7 @@ paragraph
raw: '<https://example.com>',
text: 'https://example.com',
href: 'https://example.com',
autolink: true,
tokens: [
{
type: 'text',
Expand All @@ -2064,6 +2065,7 @@ paragraph
raw: '<test@example.com>',
text: 'test@example.com',
href: 'mailto:test@example.com',
autolink: true,
tokens: [
{
type: 'text',
Expand All @@ -2085,6 +2087,7 @@ paragraph
raw: 'https://example.com',
text: 'https://example.com',
href: 'https://example.com',
autolink: true,
tokens: [
{
type: 'text',
Expand All @@ -2107,6 +2110,7 @@ paragraph
raw: 'test@example.com',
text: 'test@example.com',
href: 'mailto:test@example.com',
autolink: true,
tokens: [
{
type: 'text',
Expand Down