Skip to content

Commit

Permalink
rebber: escape link URL
Browse files Browse the repository at this point in the history
Close #114
  • Loading branch information
vhf committed Jul 7, 2017
1 parent 022023d commit 0482006
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
2 changes: 2 additions & 0 deletions packages/rebber/__tests__/__snapshots__/index.js.snap
Expand Up @@ -9,6 +9,8 @@ a paragraph\\\\footnote{footnoteRawPar inner}
"
`;

exports[`link with special characters 1`] = `"\\\\externalLink{foo}{http://example.com?a=b\\\\%c\\\\textasciicircum{}\\\\{\\\\}\\\\#foo}"`;
exports[`mix-1 1`] = `
"(for convenience, are replaced with simple single spaces in the tests)
Expand Down
17 changes: 14 additions & 3 deletions packages/rebber/__tests__/index.js
Expand Up @@ -231,6 +231,17 @@ test('link', () => {
expect(contents.trim()).toEqual(spec.expected.trim())
})

test('link with special characters', () => {
const {contents} = unified()
.use(reParse)
.use(rebber)
.processSync(dedent`
[foo](http://example.com?a=b%c^{}#foo)
`)

expect(contents.trim()).toMatchSnapshot()
})

test('link-prepend', () => {
const spec = specs['link-prepend']

Expand Down Expand Up @@ -277,11 +288,11 @@ test('footnotes', () => {
.use(rebber, integrationConfig)
.processSync(dedent`
# mytitle[^footnoteRef]
[^fotnoteRef]: reference in title
# mytitle[^footnoterawhead inner]
a paragraph[^footnoteRawPar inner]`)
expect(contents).toMatchSnapshot()
})
Expand Down
5 changes: 3 additions & 2 deletions packages/rebber/src/types/link.js
@@ -1,11 +1,12 @@
/* Dependencies. */
const has = require('has')
const all = require('../all')
const escape = require('../escaper')

/* Expose. */
module.exports = link

const defaultMacro = (displayedText, link) => `\\externalLink{${displayedText}}{${link}}`
const defaultMacro = (displayedText, url, title) => `\\externalLink{${displayedText}}{${url}}`

/* Stringify a link `node`.
*/
Expand All @@ -14,6 +15,6 @@ function link (ctx, node) {
const config = ctx.link || {}
const macro = has(config, 'macro') ? config.macro : defaultMacro
const prefix = has(config, 'prefix') ? config.prefix : ''
const url = node.url.startsWith('/') ? prefix + node.url : node.url
const url = escape(node.url.startsWith('/') ? prefix + node.url : node.url)
return macro(all(ctx, node), url, node.title)
}

0 comments on commit 0482006

Please sign in to comment.