Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion dist/index.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1866,10 +1866,17 @@ function text_join(state) {
tokens[curr].type = 'text';
}
}
for (curr = last = 0; curr < max; curr++) {
for (curr = last = 0; curr < tokens.length; 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;

// only move forward position when left token has content
if (tokens[curr].content) {
tokens[curr + 1].position = tokens[curr].position;
}
// add up size
tokens[curr + 1].size = (tokens[curr].size || 0) + (tokens[curr + 1].size || 0);
} else {
if (curr !== last) {
tokens[last] = tokens[curr];
Expand Down Expand Up @@ -2138,6 +2145,11 @@ StateBlock.prototype.getLines = function getLines(begin, end, indent, keepLastLF
if (replaceIndentSpaceWithZWSP) {
queue[i] = new Array(first - lineStart + 1).join(ZWSP) + queue[i];
}

// Ensure a trailing LF when requested even if source lacks one
if (keepLastLF && !queue[i].endsWith('\n')) {
queue[i] += '\n';
}
}
return queue.join('');
};
Expand Down Expand Up @@ -4076,6 +4088,8 @@ function escape(state, silent) {
}
token.markup = origStr;
token.info = 'escape';
// size should reflect original source span (including backslash)
token.size = origStr.length;
}
state.pos = pos + 1;
return true;
Expand Down
16 changes: 14 additions & 2 deletions dist/markdown-it.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*! @hackmd/markdown-it 14.1.0 https://github.com/hackmdio/markdown-it @license MIT */
/*! @hackmd/markdown-it 14.1.1 https://github.com/hackmdio/markdown-it @license MIT */
(function(global, factory) {
typeof exports === "object" && typeof module !== "undefined" ? module.exports = factory() : typeof define === "function" && define.amd ? define(factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self,
global.markdownit = factory());
Expand Down Expand Up @@ -2628,10 +2628,16 @@
tokens[curr].type = "text";
}
}
for (curr = last = 0; curr < max; curr++) {
for (curr = last = 0; curr < tokens.length; 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;
// only move forward position when left token has content
if (tokens[curr].content) {
tokens[curr + 1].position = tokens[curr].position;
}
// add up size
tokens[curr + 1].size = (tokens[curr].size || 0) + (tokens[curr + 1].size || 0);
} else {
if (curr !== last) {
tokens[last] = tokens[curr];
Expand Down Expand Up @@ -2889,6 +2895,10 @@
if (replaceIndentSpaceWithZWSP) {
queue[i] = new Array(first - lineStart + 1).join(ZWSP) + queue[i];
}
// Ensure a trailing LF when requested even if source lacks one
if (keepLastLF && !queue[i].endsWith("\n")) {
queue[i] += "\n";
}
}
return queue.join("");
};
Expand Down Expand Up @@ -4672,6 +4682,8 @@
}
token.markup = origStr;
token.info = "escape";
// size should reflect original source span (including backslash)
token.size = origStr.length;
}
state.pos = pos + 1;
return true;
Expand Down
4 changes: 2 additions & 2 deletions dist/markdown-it.min.js

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions lib/rules_block/state_block.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,11 @@ StateBlock.prototype.getLines = function getLines (begin, end, indent, keepLastL
if (replaceIndentSpaceWithZWSP) {
queue[i] = new Array(first - lineStart + 1).join(ZWSP) + queue[i]
}

// Ensure a trailing LF when requested even if source lacks one
if (keepLastLF && !queue[i].endsWith('\n')) {
queue[i] += '\n'
}
}

return queue.join('')
Expand Down
9 changes: 8 additions & 1 deletion lib/rules_core/text_join.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,19 @@ export default function text_join (state) {
}
}

for (curr = last = 0; curr < max; curr++) {
for (curr = last = 0; curr < tokens.length; 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

// only move forward position when left token has content
if (tokens[curr].content) {
tokens[curr + 1].position = tokens[curr].position
}
// add up size
tokens[curr + 1].size = (tokens[curr].size || 0) + (tokens[curr + 1].size || 0)
} else {
if (curr !== last) { tokens[last] = tokens[curr] }

Expand Down
2 changes: 2 additions & 0 deletions lib/rules_inline/escape.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ export default function escape (state, silent) {

token.markup = origStr
token.info = 'escape'
// size should reflect original source span (including backslash)
token.size = origStr.length
}

state.pos = pos + 1
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hackmd/markdown-it",
"version": "14.1.0",
"version": "14.1.1",
"description": "Markdown-it - modern pluggable markdown parser.",
"keywords": [
"markdown",
Expand Down
2 changes: 2 additions & 0 deletions test/commonmark.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { relative } from 'node:path'
import { load } from 'markdown-it-testgen'
import markdownit from '../index.mjs'
import { assert } from 'chai'
import { stripZeroWidthSpaces } from './patch.mjs'

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

describe('CommonMark', function () {
const md = markdownit('commonmark')
md.use(stripZeroWidthSpaces)

generate(fileURLToPath(new URL('fixtures/commonmark/good.txt', import.meta.url)), md)
})
2 changes: 2 additions & 0 deletions test/markdown-it.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { fileURLToPath } from 'node:url'
import generate from 'markdown-it-testgen'
import markdownit from '../index.mjs'
import { stripZeroWidthSpaces } from './patch.mjs'

describe('markdown-it', function () {
const md = markdownit({
Expand All @@ -9,6 +10,7 @@ describe('markdown-it', function () {
typographer: true,
linkify: true
})
md.use(stripZeroWidthSpaces)

generate(fileURLToPath(new URL('fixtures/markdown-it', import.meta.url)), md)
})
2 changes: 2 additions & 0 deletions test/misc.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { assert } from 'chai'
import markdownit from '../index.mjs'
import forInline from 'markdown-it-for-inline'
import { stripZeroWidthSpaces } from './patch.mjs'

describe('API', function () {
it('constructor', function () {
Expand Down Expand Up @@ -209,6 +210,7 @@ describe('Misc', function () {

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

assert.strictEqual(md.render('123'), '<p>123</p>\n')
assert.strictEqual(md.render('123\n'), '<p>123</p>\n')
Expand Down
8 changes: 8 additions & 0 deletions test/patch.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function stripZeroWidthSpaces (md) {
const originalRender = md.render.bind(md)

md.render = (src, env) => {
const result = originalRender(src, env)
return result.replace(/\u200b/g, '')
}
}