Skip to content

Commit dc00a59

Browse files
committed
Fix (double) encoding on urls
In f21c1b1, an algorithm was pulled in that *also* encodes. We’re operating on the AST here, encoding will be handled at the end. As it happened here, things were encoded twice. Closes GH-67.
1 parent c393d0a commit dc00a59

File tree

8 files changed

+23
-13
lines changed

8 files changed

+23
-13
lines changed

lib/footer.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* @typedef {import('./index.js').H} H
55
*/
66

7-
import {sanitizeUri} from 'micromark-util-sanitize-uri'
7+
import {normalizeUri} from 'micromark-util-sanitize-uri'
88
import {u} from 'unist-builder'
99
import {all} from './traverse.js'
1010
import {wrap} from './wrap.js'
@@ -27,7 +27,7 @@ export function footer(h) {
2727

2828
const content = all(h, def)
2929
const id = String(def.identifier)
30-
const safeId = sanitizeUri(id.toLowerCase())
30+
const safeId = normalizeUri(id.toLowerCase())
3131
let referenceIndex = 0
3232
/** @type {Array<ElementContent>} */
3333
const backReferences = []

lib/handlers/footnote-reference.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @typedef {import('../index.js').Handler} Handler
44
*/
55

6-
import {sanitizeUri} from 'micromark-util-sanitize-uri'
6+
import {normalizeUri} from 'micromark-util-sanitize-uri'
77
import {u} from 'unist-builder'
88

99
/**
@@ -12,7 +12,7 @@ import {u} from 'unist-builder'
1212
*/
1313
export function footnoteReference(h, node) {
1414
const id = String(node.identifier)
15-
const safeId = sanitizeUri(id.toLowerCase())
15+
const safeId = normalizeUri(id.toLowerCase())
1616
const index = h.footnoteOrder.indexOf(id)
1717
/** @type {number} */
1818
let counter

lib/handlers/image-reference.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* @typedef {import('../index.js').Handler} Handler
55
*/
66

7-
import {sanitizeUri} from 'micromark-util-sanitize-uri'
7+
import {normalizeUri} from 'micromark-util-sanitize-uri'
88
import {revert} from '../revert.js'
99

1010
/**
@@ -19,7 +19,7 @@ export function imageReference(h, node) {
1919
}
2020

2121
/** @type {Properties} */
22-
const props = {src: sanitizeUri(def.url || ''), alt: node.alt}
22+
const props = {src: normalizeUri(def.url || ''), alt: node.alt}
2323

2424
if (def.title !== null && def.title !== undefined) {
2525
props.title = def.title

lib/handlers/image.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
* @typedef {import('../index.js').Handler} Handler
55
*/
66

7-
import {sanitizeUri} from 'micromark-util-sanitize-uri'
7+
import {normalizeUri} from 'micromark-util-sanitize-uri'
88

99
/**
1010
* @type {Handler}
1111
* @param {Image} node
1212
*/
1313
export function image(h, node) {
1414
/** @type {Properties} */
15-
const props = {src: sanitizeUri(node.url), alt: node.alt}
15+
const props = {src: normalizeUri(node.url), alt: node.alt}
1616

1717
if (node.title !== null && node.title !== undefined) {
1818
props.title = node.title

lib/handlers/link-reference.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* @typedef {import('../index.js').Handler} Handler
55
*/
66

7-
import {sanitizeUri} from 'micromark-util-sanitize-uri'
7+
import {normalizeUri} from 'micromark-util-sanitize-uri'
88
import {revert} from '../revert.js'
99
import {all} from '../traverse.js'
1010

@@ -20,7 +20,7 @@ export function linkReference(h, node) {
2020
}
2121

2222
/** @type {Properties} */
23-
const props = {href: sanitizeUri(def.url || '')}
23+
const props = {href: normalizeUri(def.url || '')}
2424

2525
if (def.title !== null && def.title !== undefined) {
2626
props.title = def.title

lib/handlers/link.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* @typedef {import('../index.js').Handler} Handler
55
*/
66

7-
import {sanitizeUri} from 'micromark-util-sanitize-uri'
7+
import {normalizeUri} from 'micromark-util-sanitize-uri'
88
import {all} from '../traverse.js'
99

1010
/**
@@ -13,7 +13,7 @@ import {all} from '../traverse.js'
1313
*/
1414
export function link(h, node) {
1515
/** @type {Properties} */
16-
const props = {href: sanitizeUri(node.url)}
16+
const props = {href: normalizeUri(node.url)}
1717

1818
if (node.title !== null && node.title !== undefined) {
1919
props.title = node.title

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"@types/hast": "^2.0.0",
3939
"@types/mdast": "^3.0.0",
4040
"mdast-util-definitions": "^5.0.0",
41-
"micromark-util-sanitize-uri": "^1.0.0",
41+
"micromark-util-sanitize-uri": "^1.1.0",
4242
"trim-lines": "^3.0.0",
4343
"unist-builder": "^3.0.0",
4444
"unist-util-generated": "^2.0.0",

test/link.js

+10
Original file line numberDiff line numberDiff line change
@@ -74,5 +74,15 @@ test('Link', (t) => {
7474
'should correctly decode/encode urls'
7575
)
7676

77+
t.deepEqual(
78+
toHast(u('link', {url: 'https://a.com/b.png#c=d&e=f'}, [u('text', 'a')])),
79+
u(
80+
'element',
81+
{tagName: 'a', properties: {href: 'https://a.com/b.png#c=d&e=f'}},
82+
[u('text', 'a')]
83+
),
84+
'should correctly decode/encode dangerous characters'
85+
)
86+
7787
t.end()
7888
})

0 commit comments

Comments
 (0)