Browse Source

Put escape sequences into separate token

pull/864/head
Alex Kocharin 3 years ago
parent
commit
75037c6514
  1. 17
      CHANGELOG.md
  2. 5
      lib/parser_core.js
  3. 9
      lib/parser_inline.js
  4. 5
      lib/presets/commonmark.js
  5. 5
      lib/presets/zero.js
  6. 45
      lib/rules_core/text_join.js
  7. 67
      lib/rules_inline/escape.js
  8. 2
      lib/rules_inline/fragments_join.js
  9. 13
      test/fixtures/markdown-it/smartquotes.txt
  10. 17
      test/fixtures/markdown-it/typographer.txt
  11. 8
      test/misc.js

17
CHANGELOG.md

@ -5,6 +5,22 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [13.0.0] - WIP
### Added
- Added a new token type `text_special` to store escaped characters, same as `text` but
unaffected by replacement plugins (smartquotes, typographer, linkifier, etc.).
- Added a new rule `text_join` in `core` ruler. Text replacement plugins may choose to
insert themselves before it.
### Changed
- `text_collapse` rule is renamed to `fragments_join`.
### Fixed
- Smartquotes, typographic replacements and plain text links can now be escaped
with backslash (e.g. `\(c)` or `google\.com` are no longer replaced).
## [12.3.2] - 2022-01-08 ## [12.3.2] - 2022-01-08
### Security ### Security
- Fix possible ReDOS in newline rule. Thanks to @MakeNowJust. - Fix possible ReDOS in newline rule. Thanks to @MakeNowJust.
@ -592,6 +608,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Renamed presets folder (configs -> presets). - Renamed presets folder (configs -> presets).
[13.0.0]: https://github.com/markdown-it/markdown-it/compare/12.3.2...13.0.0
[12.3.2]: https://github.com/markdown-it/markdown-it/compare/12.3.1...12.3.2 [12.3.2]: https://github.com/markdown-it/markdown-it/compare/12.3.1...12.3.2
[12.3.1]: https://github.com/markdown-it/markdown-it/compare/12.3.0...12.3.1 [12.3.1]: https://github.com/markdown-it/markdown-it/compare/12.3.0...12.3.1
[12.3.0]: https://github.com/markdown-it/markdown-it/compare/12.2.0...12.3.0 [12.3.0]: https://github.com/markdown-it/markdown-it/compare/12.2.0...12.3.0

5
lib/parser_core.js

@ -16,7 +16,10 @@ var _rules = [
[ 'inline', require('./rules_core/inline') ], [ 'inline', require('./rules_core/inline') ],
[ 'linkify', require('./rules_core/linkify') ], [ 'linkify', require('./rules_core/linkify') ],
[ 'replacements', require('./rules_core/replacements') ], [ 'replacements', require('./rules_core/replacements') ],
[ 'smartquotes', require('./rules_core/smartquotes') ] [ 'smartquotes', require('./rules_core/smartquotes') ],
// `text_join` finds `text_special` tokens (for escape sequences)
// and joins them with the rest of the text
[ 'text_join', require('./rules_core/text_join') ]
]; ];

9
lib/parser_inline.js

@ -26,11 +26,18 @@ var _rules = [
[ 'entity', require('./rules_inline/entity') ] [ 'entity', require('./rules_inline/entity') ]
]; ];
// `rule2` ruleset was created specifically for emphasis/strikethrough
// post-processing and may be changed in the future.
//
// Don't use this for anything except pairs (plugins working with `balance_pairs`).
//
var _rules2 = [ var _rules2 = [
[ 'balance_pairs', require('./rules_inline/balance_pairs') ], [ 'balance_pairs', require('./rules_inline/balance_pairs') ],
[ 'strikethrough', require('./rules_inline/strikethrough').postProcess ], [ 'strikethrough', require('./rules_inline/strikethrough').postProcess ],
[ 'emphasis', require('./rules_inline/emphasis').postProcess ], [ 'emphasis', require('./rules_inline/emphasis').postProcess ],
[ 'text_collapse', require('./rules_inline/text_collapse') ] // rules for pairs separate '**' into its own text tokens, which may be left unused,
// rule below merges unused segments back with the rest of the text
[ 'fragments_join', require('./rules_inline/fragments_join') ]
]; ];

5
lib/presets/commonmark.js

@ -38,7 +38,8 @@ module.exports = {
rules: [ rules: [
'normalize', 'normalize',
'block', 'block',
'inline' 'inline',
'text_join'
] ]
}, },
@ -73,7 +74,7 @@ module.exports = {
rules2: [ rules2: [
'balance_pairs', 'balance_pairs',
'emphasis', 'emphasis',
'text_collapse' 'fragments_join'
] ]
} }
} }

5
lib/presets/zero.js

@ -39,7 +39,8 @@ module.exports = {
rules: [ rules: [
'normalize', 'normalize',
'block', 'block',
'inline' 'inline',
'text_join'
] ]
}, },
@ -55,7 +56,7 @@ module.exports = {
], ],
rules2: [ rules2: [
'balance_pairs', 'balance_pairs',
'text_collapse' 'fragments_join'
] ]
} }
} }

45
lib/rules_core/text_join.js

@ -0,0 +1,45 @@
// Join raw text tokens with the rest of the text
//
// This is set as a separate rule to provide an opportunity for plugins
// to run text replacements after text join, but before escape join.
//
// For example, `\:)` shouldn't be replaced with an emoji.
//
'use strict';
module.exports = function text_join(state) {
var j, l, tokens, curr, max, last,
blockTokens = state.tokens;
for (j = 0, l = blockTokens.length; j < l; j++) {
if (blockTokens[j].type !== 'inline') continue;
tokens = blockTokens[j].children;
max = tokens.length;
for (curr = 0; curr < max; curr++) {
if (tokens[curr].type === 'text_special') {
tokens[curr].type = 'text';
}
}
for (curr = last = 0; curr < max; curr++) {
if (tokens[curr].type === 'text' &&
curr + 1 < max &&
tokens[curr + 1].type === 'text') {
// collapse two adjacent text nodes
tokens[curr + 1].content = tokens[curr].content + tokens[curr + 1].content;
} else {
if (curr !== last) { tokens[last] = tokens[curr]; }
last++;
}
}
if (curr !== last) {
tokens.length = last;
}
}
};

67
lib/rules_inline/escape.js

@ -13,40 +13,59 @@ for (var i = 0; i < 256; i++) { ESCAPED.push(0); }
module.exports = function escape(state, silent) { module.exports = function escape(state, silent) {
var ch, pos = state.pos, max = state.posMax; var ch1, ch2, origStr, escapedStr, token, pos = state.pos, max = state.posMax;
if (state.src.charCodeAt(pos) !== 0x5C/* \ */) { return false; }
if (state.src.charCodeAt(pos) !== 0x5C/* \ */) return false;
pos++; pos++;
if (pos < max) { // '\' at the end of the inline block
ch = state.src.charCodeAt(pos); if (pos >= max) return false;
ch1 = state.src.charCodeAt(pos);
if (ch < 256 && ESCAPED[ch] !== 0) { if (ch1 === 0x0A) {
if (!silent) { state.pending += state.src[pos]; } if (!silent) {
state.pos += 2; state.push('hardbreak', 'br', 0);
return true;
} }
if (ch === 0x0A) { pos++;
if (!silent) { // skip leading whitespaces from next line
state.push('hardbreak', 'br', 0); while (pos < max) {
} ch1 = state.src.charCodeAt(pos);
if (!isSpace(ch1)) break;
pos++;
}
state.pos = pos;
return true;
}
escapedStr = state.src[pos];
if (ch1 >= 0xD800 && ch1 <= 0xDBFF && pos + 1 < max) {
ch2 = state.src.charCodeAt(pos + 1);
if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) {
escapedStr += state.src[pos + 1];
pos++; pos++;
// skip leading whitespaces from next line
while (pos < max) {
ch = state.src.charCodeAt(pos);
if (!isSpace(ch)) { break; }
pos++;
}
state.pos = pos;
return true;
} }
} }
if (!silent) { state.pending += '\\'; } origStr = '\\' + escapedStr;
state.pos++;
if (!silent) {
token = state.push('text_special', '', 0);
if (ch1 < 256 && ESCAPED[ch1] !== 0) {
token.content = escapedStr;
} else {
token.content = origStr;
}
token.markup = origStr;
token.info = 'escape';
}
state.pos = pos + 1;
return true; return true;
}; };

2
lib/rules_inline/text_collapse.js → lib/rules_inline/fragments_join.js

@ -9,7 +9,7 @@
'use strict'; 'use strict';
module.exports = function text_collapse(state) { module.exports = function fragments_join(state) {
var curr, last, var curr, last,
level = 0, level = 0,
tokens = state.tokens, tokens = state.tokens,

13
test/fixtures/markdown-it/smartquotes.txt

@ -164,3 +164,16 @@ Should parse quotes adjacent to inline html, #677:
<p>“test <br>”</p> <p>“test <br>”</p>
<p>“<br> test”</p> <p>“<br> test”</p>
. .
Should be escapable:
.
"foo"
\"foo"
"foo\"
.
<p>“foo”</p>
<p>&quot;foo&quot;</p>
<p>&quot;foo&quot;</p>
.

17
test/fixtures/markdown-it/typographer.txt

@ -60,6 +60,13 @@ dupes
<p>!!! ??? ,</p> <p>!!! ??? ,</p>
. .
copyright should be escapable
.
\(c)
.
<p>(c)</p>
.
dashes dashes
. .
@ -80,6 +87,16 @@ markdownit--awesome
<p>markdownit–awesome</p> <p>markdownit–awesome</p>
. .
dashes should be escapable
.
foo \-- bar
foo -\- bar
.
<p>foo -- bar</p>
<p>foo -- bar</p>
.
regression tests for #624 regression tests for #624
. .
1---2---3 1---2---3

8
test/misc.js

@ -254,6 +254,14 @@ describe('Misc', function () {
md.render('# test\n\n - hello\n - world\n') md.render('# test\n\n - hello\n - world\n')
); );
}); });
it('Should escape surrogate pairs (coverage)', function () {
var md = markdownit();
assert.strictEqual(md.render('\\\uD835\uDC9C'), '<p>\\\uD835\uDC9C</p>\n');
assert.strictEqual(md.render('\\\uD835x'), '<p>\\\uD835x</p>\n');
assert.strictEqual(md.render('\\\uD835'), '<p>\\\uD835</p>\n');
});
}); });

Loading…
Cancel
Save