mdast-util-to-markdown
Advanced tools
Comparing version 0.6.2 to 0.6.3
module.exports = html | ||
html.peek = htmlPeek | ||
@@ -6,1 +7,5 @@ function html(node) { | ||
} | ||
function htmlPeek() { | ||
return '<' | ||
} |
module.exports = inlineCode | ||
inlineCode.peek = inlineCodePeek | ||
function inlineCode(node) { | ||
var compilePattern = require('../util/compile-pattern') | ||
function inlineCode(node, parent, context) { | ||
var value = node.value || '' | ||
var sequence = '`' | ||
var pad = '' | ||
var index = -1 | ||
var pattern | ||
var expression | ||
var match | ||
var position | ||
@@ -23,6 +29,38 @@ // If there is a single grave accent on its own in the code, use a fence of | ||
) { | ||
pad = ' ' | ||
value = ' ' + value + ' ' | ||
} | ||
return sequence + pad + value + pad + sequence | ||
// We have a potential problem: certain characters after eols could result in | ||
// blocks being seen. | ||
// For example, if someone injected the string `'\n# b'`, then that would | ||
// result in an ATX heading. | ||
// We can’t escape characters in `inlineCode`, but because eols are | ||
// transformed to spaces when going from markdown to HTML anyway, we can swap | ||
// them out. | ||
while (++index < context.unsafe.length) { | ||
pattern = context.unsafe[index] | ||
// Only look for `atBreak`s. | ||
// Btw: note that `atBreak` patterns will always start the regex at LF or | ||
// CR. | ||
if (!pattern.atBreak) continue | ||
expression = compilePattern(pattern) | ||
while ((match = expression.exec(value))) { | ||
position = match.index | ||
// Support CRLF (patterns only look for one of the characters). | ||
if ( | ||
value.charCodeAt(position) === 10 /* `\n` */ && | ||
value.charCodeAt(position - 1) === 13 /* `\r` */ | ||
) { | ||
position-- | ||
} | ||
value = value.slice(0, position) + ' ' + value.slice(match.index + 1) | ||
} | ||
} | ||
return sequence + value + sequence | ||
} | ||
@@ -29,0 +67,0 @@ |
@@ -85,3 +85,3 @@ module.exports = [ | ||
// hard break (when followed by an eol). | ||
{character: '\\', after: '[!-/:-@[-`{-~]'}, | ||
{character: '\\', after: '[!-/:-@[-`{-~]', collapse: false}, | ||
{character: '\\', after: '[\\r\\n]', inConstruct: 'phrasing'}, | ||
@@ -88,0 +88,0 @@ // A right bracket can exit labels. |
@@ -28,2 +28,16 @@ module.exports = phrasing | ||
// In some cases, html (text) can be found in phrasing right after an eol. | ||
// When we’d serialize that, in most cases that would be seen as html | ||
// (flow). | ||
// As we can’t escape or so to prevent it from happening, we take a somewhat | ||
// reasonable approach: replace that eol with a space. | ||
// See: <https://github.com/syntax-tree/mdast-util-to-markdown/issues/15> | ||
if ((before === '\r' || before === '\n') && child.type === 'html') { | ||
results[results.length - 1] = results[results.length - 1].replace( | ||
/(\r?\n|\r)$/, | ||
' ' | ||
) | ||
before = ' ' | ||
} | ||
results.push( | ||
@@ -35,2 +49,3 @@ context.handle(child, parent, context, { | ||
) | ||
before = results[results.length - 1].slice(-1) | ||
@@ -37,0 +52,0 @@ } |
module.exports = safe | ||
var compilePattern = require('./compile-pattern') | ||
function safe(context, input, config) { | ||
@@ -28,8 +30,20 @@ var value = (config.before || '') + (input || '') + (config.after || '') | ||
expression = | ||
pattern._compiled || (pattern._compiled = toExpression(pattern)) | ||
expression = compilePattern(pattern) | ||
while ((match = expression.exec(value))) { | ||
before = 'before' in pattern || pattern.atBreak | ||
after = 'after' in pattern | ||
// Often, patterns which depend on a character before or after it, such | ||
// as `!` when followed by `[` (for an image), do not have to be escaped | ||
// when the other character has to be escaped as well. | ||
// But in the case of a backslash, such as `\` when followed by `*`, that | ||
// is not correct: both have to be escaped. | ||
// This is a naïve “fix” for that though, but this seems the simplest for | ||
// now. | ||
if (pattern.collapse === false) { | ||
before = false | ||
after = false | ||
} else { | ||
before = 'before' in pattern || pattern.atBreak | ||
after = 'after' in pattern | ||
} | ||
position = match.index + (before ? match[1].length : 0) | ||
@@ -130,21 +144,4 @@ | ||
function toExpression(pattern) { | ||
var before = pattern.before ? '(?:' + pattern.before + ')' : '' | ||
var after = pattern.after ? '(?:' + pattern.after + ')' : '' | ||
if (pattern.atBreak) { | ||
before = '[\\r\\n][\\t ]*' + before | ||
} | ||
return new RegExp( | ||
(before ? '(' + before + ')' : '') + | ||
(/[|\\{}()[\]^$+*?.-]/.test(pattern.character) ? '\\' : '') + | ||
pattern.character + | ||
(after || ''), | ||
'g' | ||
) | ||
} | ||
function numerical(a, b) { | ||
return a - b | ||
} |
{ | ||
"name": "mdast-util-to-markdown", | ||
"version": "0.6.2", | ||
"version": "0.6.3", | ||
"description": "mdast utility to serialize markdown", | ||
@@ -55,3 +55,4 @@ "license": "MIT", | ||
"tinyify": "^3.0.0", | ||
"xo": "^0.36.0" | ||
"unist-util-remove-position": "^3.0.0", | ||
"xo": "^0.37.0" | ||
}, | ||
@@ -58,0 +59,0 @@ "scripts": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
50095
46
1220
11