Skip to content

Commit

Permalink
Implement inline link serialization that doesn't wrap them in angle (#…
Browse files Browse the repository at this point in the history
…1171)

brackets

Fixes #1169
  • Loading branch information
johno committed Jul 22, 2020
1 parent d08c5b6 commit 649dbd8
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/remark-mdx/lib/serialize/index.js
Expand Up @@ -2,6 +2,7 @@

var mdxExpression = require('./mdx-expression')
var mdxElement = require('./mdx-element')
var link = require('./link')
var text = require('./text')

module.exports = serialize
Expand All @@ -18,5 +19,6 @@ function serialize(compiler) {
visitors.mdxBlockExpression = mdxExpression
visitors.mdxSpanElement = mdxElement
visitors.mdxBlockElement = mdxElement
visitors.link = link
visitors.text = text
}
65 changes: 65 additions & 0 deletions packages/remark-mdx/lib/serialize/link.js
@@ -0,0 +1,65 @@
'use strict'

// Copied and modified from remark-stringify
// Original source: https://github.com/remarkjs/remark/blob/19a27c30b13baa3a9e864e528c6a0b901a5fc918/packages/remark-stringify/lib/visitors/link.js
// License (MIT): https://github.com/remarkjs/remark/blob/main/license

var uri = require('remark-stringify/lib/util/enclose-uri')
var title = require('remark-stringify/lib/util/enclose-title')

module.exports = link

var space = ' '
var leftSquareBracket = '['
var rightSquareBracket = ']'
var leftParenthesis = '('
var rightParenthesis = ')'

// Expression for a protocol:
// See <https://en.wikipedia.org/wiki/Uniform_Resource_Identifier#Generic_syntax>.
var protocol = /^[a-z][a-z+.-]+:\/?/i

// Stringify a link.
//
// With MDX we don't support <https://mdxjs.com> style links, which is the default
// for remark-stringify (understandably). So, we check for the case where the url
// and its text are equal. If it is, we serialize/stringify link syntax with the
// url as the title rather than wrapping it in angle brackets (<>).
//
// ```markdown
// [http://example.com](http://example.com)
// ```
//
// Otherwise, is smart about enclosing `url` (see `encloseURI()`) and `title`
// (see `encloseTitle()`).
// ```
//
// ```markdown
// [foo](<foo at bar dot com> 'An "example" e-mail')
// ```
//
// Supports named entities in the `url` and `title` when in `settings.encode`
// mode.
function link(node) {
var self = this
var content = self.encode(node.url || '', node)
var exit = self.enterLink()
var value = self.all(node).join('')

exit()

content = uri(content)

if (node.title) {
content += space + title(self.encode(self.escape(node.title, node), node))
}

return (
leftSquareBracket +
value +
rightSquareBracket +
leftParenthesis +
content +
rightParenthesis
)
}
10 changes: 10 additions & 0 deletions packages/remark-mdx/test/index.js
Expand Up @@ -1302,6 +1302,16 @@ test('stringify', function (t) {
'should serialize an expression attribute (element)'
)

t.equal(
basic.stringify(
u('paragraph', [
u('link', {url: 'https://mdxjs.com'}, [u('text', 'https://mdxjs.com')])
])
),
'[https://mdxjs.com](https://mdxjs.com)',
'should write out a url as the raw value'
)

t.equal(
basic.stringify(
u('paragraph', [
Expand Down

0 comments on commit 649dbd8

Please sign in to comment.