Skip to content

Remove URL escaping in link labels #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 4, 2021
Merged
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
235 changes: 235 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,245 @@ test('markdown -> mdast', function (t) {
'should support autolink literals'
)

t.deepEqual(
fromMarkdown(
`[http://localhost]: http://localhost "Open example on localhost"
[https://dev.local]: https://dev.local "Open example on localhost"

Existing link [http://localhost][], [https://dev.local][] in paragraph.`,
{
extensions: [syntax],
mdastExtensions: [autolinkLiterals.fromMarkdown]
}
),
{
type: 'root',
children: [
{
type: 'definition',
identifier: 'http://localhost',
label: 'http://localhost',
title: 'Open example on localhost',
url: 'http://localhost',
position: {
start: {line: 1, column: 1, offset: 0},
end: {line: 1, column: 65, offset: 64}
}
},
{
type: 'definition',
identifier: 'https://dev.local',
label: 'https://dev.local',
title: 'Open example on localhost',
url: 'https://dev.local',
position: {
start: {line: 2, column: 1, offset: 65},
end: {line: 2, column: 67, offset: 131}
}
},
{
type: 'paragraph',
children: [
{
type: 'text',
value: 'Existing link ',
position: {
start: {line: 4, column: 1, offset: 133},
end: {line: 4, column: 15, offset: 147}
}
},
{
type: 'linkReference',
children: [
{
type: 'text',
value: 'http://localhost',
position: {
start: {line: 4, column: 16, offset: 148},
end: {line: 4, column: 32, offset: 164}
}
}
],
position: {
start: {line: 4, column: 15, offset: 147},
end: {line: 4, column: 35, offset: 167}
},
identifier: 'http://localhost',
label: 'http://localhost',
referenceType: 'collapsed'
},
{
type: 'text',
value: ', ',
position: {
start: {line: 4, column: 35, offset: 167},
end: {line: 4, column: 37, offset: 169}
}
},
{
type: 'linkReference',
children: [
{
type: 'text',
value: 'https://dev.local',
position: {
start: {line: 4, column: 38, offset: 170},
end: {line: 4, column: 55, offset: 187}
}
}
],
position: {
start: {line: 4, column: 37, offset: 169},
end: {line: 4, column: 58, offset: 190}
},
identifier: 'https://dev.local',
label: 'https://dev.local',
referenceType: 'collapsed'
},
{
type: 'text',
value: ' in paragraph.',
position: {
start: {line: 4, column: 58, offset: 190},
end: {line: 4, column: 72, offset: 204}
}
}
],
position: {
start: {line: 4, column: 1, offset: 133},
end: {line: 4, column: 72, offset: 204}
}
}
],
position: {
start: {line: 1, column: 1, offset: 0},
end: {line: 4, column: 72, offset: 204}
}
},
'should support existing link references with identifier as label'
)

t.end()
})

test('mdast -> markdown', function (t) {
t.deepEqual(
toMarkdown(
{
type: 'root',
children: [
{
type: 'definition',
identifier: 'http://localhost',
label: 'http://localhost',
title: 'Open example on localhost',
url: 'http://localhost',
position: {
start: {line: 1, column: 1, offset: 0},
end: {line: 1, column: 65, offset: 64}
}
},
{
type: 'definition',
identifier: 'https://dev.local',
label: 'https://dev.local',
title: 'Open example on localhost',
url: 'https://dev.local',
position: {
start: {line: 2, column: 1, offset: 65},
end: {line: 2, column: 67, offset: 131}
}
},
{
type: 'paragraph',
children: [
{
type: 'text',
value: 'Existing link ',
position: {
start: {line: 4, column: 1, offset: 133},
end: {line: 4, column: 15, offset: 147}
}
},
{
type: 'linkReference',
children: [
{
type: 'text',
value: 'http://localhost',
position: {
start: {line: 4, column: 16, offset: 148},
end: {line: 4, column: 32, offset: 164}
}
}
],
position: {
start: {line: 4, column: 15, offset: 147},
end: {line: 4, column: 35, offset: 167}
},
identifier: 'http://localhost',
label: 'http://localhost',
referenceType: 'collapsed'
},
{
type: 'text',
value: ', ',
position: {
start: {line: 4, column: 35, offset: 167},
end: {line: 4, column: 37, offset: 169}
}
},
{
type: 'linkReference',
children: [
{
type: 'text',
value: 'https://dev.local',
position: {
start: {line: 4, column: 38, offset: 170},
end: {line: 4, column: 55, offset: 187}
}
}
],
position: {
start: {line: 4, column: 37, offset: 169},
end: {line: 4, column: 58, offset: 190}
},
identifier: 'https://dev.local',
label: 'https://dev.local',
referenceType: 'collapsed'
},
{
type: 'text',
value: ' in paragraph.',
position: {
start: {line: 4, column: 58, offset: 190},
end: {line: 4, column: 72, offset: 204}
}
}
],
position: {
start: {line: 4, column: 1, offset: 133},
end: {line: 4, column: 72, offset: 204}
}
}
],
position: {
start: {line: 1, column: 1, offset: 0},
end: {line: 4, column: 72, offset: 204}
}
},
{extensions: [autolinkLiterals.toMarkdown]}
),
`[http://localhost]: http://localhost "Open example on localhost"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only thing that still disturbes me a bit is the additional whiteline here, but that is not related to the current issue I resolved.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it’s a good default but it’s configurable: https://github.com/syntax-tree/mdast-util-to-markdown#optionstightdefinitions

[https://dev.local]: https://dev.local "Open example on localhost"

Existing link [http://localhost][], [https://dev.local][] in paragraph.
`,
'should not escape existing link text.'
)

t.deepEqual(
toMarkdown(
{
Expand Down
2 changes: 1 addition & 1 deletion to-markdown.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var inConstruct = 'phrasing'
var notInConstruct = ['autolink', 'link', 'image']
var notInConstruct = ['autolink', 'link', 'image', 'label']

exports.unsafe = [
{
Expand Down