Skip to content

Commit b412edd

Browse files
authored
Merge pull request #19 from stanley2058/master
fix/handle `text_special` and `text_join` rule causing incorrect position and size with escaped links
2 parents a62b5cb + e561afb commit b412edd

File tree

11 files changed

+61
-7
lines changed

11 files changed

+61
-7
lines changed

dist/index.cjs.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1866,10 +1866,17 @@ function text_join(state) {
18661866
tokens[curr].type = 'text';
18671867
}
18681868
}
1869-
for (curr = last = 0; curr < max; curr++) {
1869+
for (curr = last = 0; curr < tokens.length; curr++) {
18701870
if (tokens[curr].type === 'text' && curr + 1 < max && tokens[curr + 1].type === 'text') {
18711871
// collapse two adjacent text nodes
18721872
tokens[curr + 1].content = tokens[curr].content + tokens[curr + 1].content;
1873+
1874+
// only move forward position when left token has content
1875+
if (tokens[curr].content) {
1876+
tokens[curr + 1].position = tokens[curr].position;
1877+
}
1878+
// add up size
1879+
tokens[curr + 1].size = (tokens[curr].size || 0) + (tokens[curr + 1].size || 0);
18731880
} else {
18741881
if (curr !== last) {
18751882
tokens[last] = tokens[curr];
@@ -2138,6 +2145,11 @@ StateBlock.prototype.getLines = function getLines(begin, end, indent, keepLastLF
21382145
if (replaceIndentSpaceWithZWSP) {
21392146
queue[i] = new Array(first - lineStart + 1).join(ZWSP) + queue[i];
21402147
}
2148+
2149+
// Ensure a trailing LF when requested even if source lacks one
2150+
if (keepLastLF && !queue[i].endsWith('\n')) {
2151+
queue[i] += '\n';
2152+
}
21412153
}
21422154
return queue.join('');
21432155
};
@@ -4076,6 +4088,8 @@ function escape(state, silent) {
40764088
}
40774089
token.markup = origStr;
40784090
token.info = 'escape';
4091+
// size should reflect original source span (including backslash)
4092+
token.size = origStr.length;
40794093
}
40804094
state.pos = pos + 1;
40814095
return true;

dist/markdown-it.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*! @hackmd/markdown-it 14.1.0 https://github.com/hackmdio/markdown-it @license MIT */
1+
/*! @hackmd/markdown-it 14.1.1 https://github.com/hackmdio/markdown-it @license MIT */
22
(function(global, factory) {
33
typeof exports === "object" && typeof module !== "undefined" ? module.exports = factory() : typeof define === "function" && define.amd ? define(factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self,
44
global.markdownit = factory());
@@ -2628,10 +2628,16 @@
26282628
tokens[curr].type = "text";
26292629
}
26302630
}
2631-
for (curr = last = 0; curr < max; curr++) {
2631+
for (curr = last = 0; curr < tokens.length; curr++) {
26322632
if (tokens[curr].type === "text" && curr + 1 < max && tokens[curr + 1].type === "text") {
26332633
// collapse two adjacent text nodes
26342634
tokens[curr + 1].content = tokens[curr].content + tokens[curr + 1].content;
2635+
// only move forward position when left token has content
2636+
if (tokens[curr].content) {
2637+
tokens[curr + 1].position = tokens[curr].position;
2638+
}
2639+
// add up size
2640+
tokens[curr + 1].size = (tokens[curr].size || 0) + (tokens[curr + 1].size || 0);
26352641
} else {
26362642
if (curr !== last) {
26372643
tokens[last] = tokens[curr];
@@ -2889,6 +2895,10 @@
28892895
if (replaceIndentSpaceWithZWSP) {
28902896
queue[i] = new Array(first - lineStart + 1).join(ZWSP) + queue[i];
28912897
}
2898+
// Ensure a trailing LF when requested even if source lacks one
2899+
if (keepLastLF && !queue[i].endsWith("\n")) {
2900+
queue[i] += "\n";
2901+
}
28922902
}
28932903
return queue.join("");
28942904
};
@@ -4672,6 +4682,8 @@
46724682
}
46734683
token.markup = origStr;
46744684
token.info = "escape";
4685+
// size should reflect original source span (including backslash)
4686+
token.size = origStr.length;
46754687
}
46764688
state.pos = pos + 1;
46774689
return true;

dist/markdown-it.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/rules_block/state_block.mjs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,11 @@ StateBlock.prototype.getLines = function getLines (begin, end, indent, keepLastL
224224
if (replaceIndentSpaceWithZWSP) {
225225
queue[i] = new Array(first - lineStart + 1).join(ZWSP) + queue[i]
226226
}
227+
228+
// Ensure a trailing LF when requested even if source lacks one
229+
if (keepLastLF && !queue[i].endsWith('\n')) {
230+
queue[i] += '\n'
231+
}
227232
}
228233

229234
return queue.join('')

lib/rules_core/text_join.mjs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,19 @@ export default function text_join (state) {
2323
}
2424
}
2525

26-
for (curr = last = 0; curr < max; curr++) {
26+
for (curr = last = 0; curr < tokens.length; curr++) {
2727
if (tokens[curr].type === 'text' &&
2828
curr + 1 < max &&
2929
tokens[curr + 1].type === 'text') {
3030
// collapse two adjacent text nodes
3131
tokens[curr + 1].content = tokens[curr].content + tokens[curr + 1].content
32+
33+
// only move forward position when left token has content
34+
if (tokens[curr].content) {
35+
tokens[curr + 1].position = tokens[curr].position
36+
}
37+
// add up size
38+
tokens[curr + 1].size = (tokens[curr].size || 0) + (tokens[curr + 1].size || 0)
3239
} else {
3340
if (curr !== last) { tokens[last] = tokens[curr] }
3441

lib/rules_inline/escape.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ export default function escape (state, silent) {
6262

6363
token.markup = origStr
6464
token.info = 'escape'
65+
// size should reflect original source span (including backslash)
66+
token.size = origStr.length
6567
}
6668

6769
state.pos = pos + 1

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@hackmd/markdown-it",
3-
"version": "14.1.0",
3+
"version": "14.1.1",
44
"description": "Markdown-it - modern pluggable markdown parser.",
55
"keywords": [
66
"markdown",

test/commonmark.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { relative } from 'node:path'
33
import { load } from 'markdown-it-testgen'
44
import markdownit from '../index.mjs'
55
import { assert } from 'chai'
6+
import { stripZeroWidthSpaces } from './patch.mjs'
67

78
function normalize (text) {
89
return text.replace(/<blockquote>\n<\/blockquote>/g, '<blockquote></blockquote>')
@@ -26,6 +27,7 @@ function generate (path, md) {
2627

2728
describe('CommonMark', function () {
2829
const md = markdownit('commonmark')
30+
md.use(stripZeroWidthSpaces)
2931

3032
generate(fileURLToPath(new URL('fixtures/commonmark/good.txt', import.meta.url)), md)
3133
})

test/markdown-it.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { fileURLToPath } from 'node:url'
22
import generate from 'markdown-it-testgen'
33
import markdownit from '../index.mjs'
4+
import { stripZeroWidthSpaces } from './patch.mjs'
45

56
describe('markdown-it', function () {
67
const md = markdownit({
@@ -9,6 +10,7 @@ describe('markdown-it', function () {
910
typographer: true,
1011
linkify: true
1112
})
13+
md.use(stripZeroWidthSpaces)
1214

1315
generate(fileURLToPath(new URL('fixtures/markdown-it', import.meta.url)), md)
1416
})

test/misc.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { assert } from 'chai'
22
import markdownit from '../index.mjs'
33
import forInline from 'markdown-it-for-inline'
4+
import { stripZeroWidthSpaces } from './patch.mjs'
45

56
describe('API', function () {
67
it('constructor', function () {
@@ -209,6 +210,7 @@ describe('Misc', function () {
209210

210211
it('Should correctly parse strings without tailing \\n', function () {
211212
const md = markdownit()
213+
md.use(stripZeroWidthSpaces)
212214

213215
assert.strictEqual(md.render('123'), '<p>123</p>\n')
214216
assert.strictEqual(md.render('123\n'), '<p>123</p>\n')

0 commit comments

Comments
 (0)