markdown-it-critic 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE.md ADDED
@@ -0,0 +1,9 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Márton Visnovitz
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # markdown-it-critic
2
+
3
+ [![License][license-badge]][license-link]
4
+ [![Version][version-badge]][version-link]
5
+ [![Build][build-badge]][build-link]
6
+
7
+ [CriticMarkup](http://criticmarkup.com/) syntax as a [markdown-it](https://github.com/markdown-it/markdown-it)
8
+ plugin, implemented as genuine inline tokenizer rules — change-tracking spans become first-class
9
+ tokens in markdown-it's own token stream.
10
+
11
+ ## Setup
12
+
13
+ Install via `npm`:
14
+
15
+ ```bash
16
+ npm install markdown-it-critic
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ Registers like any other `markdown-it` plugin via `.use()`, no special setup, and safe to
22
+ combine with other plugins in a curated distribution (e.g. `extramark`) or a standalone
23
+ `markdown-it` instance.
24
+
25
+ ```js
26
+ import MarkdownIt from "markdown-it";
27
+ import criticMarkup from "markdown-it-critic";
28
+
29
+ const md = new MarkdownIt().use(criticMarkup);
30
+
31
+ md.render("Lorem{-- ipsum--} dolor {++sit++} amet.");
32
+ ```
33
+
34
+ ## Contributing
35
+
36
+ All ideas, recommendations, bug reports, pull requests are welcome. 😊
37
+
38
+ [license-badge]: https://img.shields.io/npm/l/markdown-it-critic.svg
39
+ [license-link]: https://github.com/vimtaai/markdown-it-critic/blob/master/LICENSE.md
40
+ [version-badge]: https://img.shields.io/npm/v/markdown-it-critic.svg
41
+ [version-link]: https://www.npmjs.com/package/markdown-it-critic
42
+ [build-badge]: https://github.com/vimtaai/markdown-it-critic/actions/workflows/main.yaml/badge.svg
43
+ [build-link]: https://github.com/vimtaai/markdown-it-critic/actions/workflows/main.yaml
package/lib/index.js ADDED
@@ -0,0 +1,12 @@
1
+ import { addition } from "./rules/addition.js";
2
+ import { comment } from "./rules/comment.js";
3
+ import { deletion } from "./rules/deletion.js";
4
+ import { highlight } from "./rules/highlight.js";
5
+ import { paragraphBreak } from "./rules/paragraph-break.js";
6
+ import { substitution } from "./rules/substitution.js";
7
+
8
+ export function markdownItCriticMarkup(md) {
9
+ md.use(comment).use(highlight).use(addition).use(deletion).use(substitution).use(paragraphBreak);
10
+ }
11
+
12
+ export default markdownItCriticMarkup;
@@ -0,0 +1,53 @@
1
+ const OPEN = "{++";
2
+ const CLOSE = "++}";
3
+
4
+ export function additionRule(state, silent) {
5
+ const start = state.pos;
6
+
7
+ if (state.src.slice(start, start + OPEN.length) !== OPEN) return false;
8
+
9
+ const closeIndex = state.src.indexOf(CLOSE, start + OPEN.length);
10
+ if (closeIndex === -1) return false;
11
+
12
+ if (silent) return true;
13
+
14
+ pushInsOpen(state, start + OPEN.length, closeIndex);
15
+ state.pos = closeIndex + CLOSE.length;
16
+ pushInsClose(state, start + OPEN.length, closeIndex);
17
+
18
+ return true;
19
+ }
20
+
21
+ export function pushInsOpen(state, contentStart, contentEnd) {
22
+ const isEmpty = state.src.slice(contentStart, contentEnd).trim() === "";
23
+ const token = state.push("critic_ins_open", "ins", 1);
24
+ token.meta = { empty: isEmpty };
25
+
26
+ if (!isEmpty) {
27
+ const maxPos = state.posMax;
28
+ state.pos = contentStart;
29
+ state.posMax = contentEnd;
30
+ state.md.inline.tokenize(state);
31
+ state.posMax = maxPos;
32
+ }
33
+ }
34
+
35
+ export function pushInsClose(state, contentStart, contentEnd) {
36
+ const isEmpty = state.src.slice(contentStart, contentEnd).trim() === "";
37
+ const token = state.push("critic_ins_close", "ins", -1);
38
+ token.meta = { empty: isEmpty };
39
+ }
40
+
41
+ export function renderInsOpen(tokens, idx) {
42
+ return tokens[idx].meta?.empty ? '<ins class="break">&nbsp;</ins>' : "<ins>";
43
+ }
44
+
45
+ export function renderInsClose(tokens, idx) {
46
+ return tokens[idx].meta?.empty ? "" : "</ins>";
47
+ }
48
+
49
+ export function addition(md) {
50
+ md.inline.ruler.push("critic_addition", additionRule);
51
+ md.renderer.rules.critic_ins_open = renderInsOpen;
52
+ md.renderer.rules.critic_ins_close = renderInsClose;
53
+ }
@@ -0,0 +1,42 @@
1
+ const OPEN = "{>>";
2
+ const CLOSE = "<<}";
3
+
4
+ function commentRule(state, silent) {
5
+ const start = state.pos;
6
+
7
+ if (state.src.slice(start, start + OPEN.length) !== OPEN) return false;
8
+
9
+ const closeIndex = state.src.indexOf(CLOSE, start + OPEN.length);
10
+ if (closeIndex === -1) return false;
11
+
12
+ if (silent) return true;
13
+
14
+ const maxPos = state.posMax;
15
+
16
+ state.push("critic_comment_open", "span", 1);
17
+
18
+ state.pos = start + OPEN.length;
19
+ state.posMax = closeIndex;
20
+ state.md.inline.tokenize(state);
21
+
22
+ state.pos = closeIndex + CLOSE.length;
23
+ state.posMax = maxPos;
24
+
25
+ state.push("critic_comment_close", "span", -1);
26
+
27
+ return true;
28
+ }
29
+
30
+ function renderCommentOpen() {
31
+ return '<span class="critic comment">';
32
+ }
33
+
34
+ function renderCommentClose() {
35
+ return "</span>";
36
+ }
37
+
38
+ export function comment(md) {
39
+ md.inline.ruler.push("critic_comment", commentRule);
40
+ md.renderer.rules.critic_comment_open = renderCommentOpen;
41
+ md.renderer.rules.critic_comment_close = renderCommentClose;
42
+ }
@@ -0,0 +1,53 @@
1
+ const OPEN = "{--";
2
+ const CLOSE = "--}";
3
+
4
+ export function deletionRule(state, silent) {
5
+ const start = state.pos;
6
+
7
+ if (state.src.slice(start, start + OPEN.length) !== OPEN) return false;
8
+
9
+ const closeIndex = state.src.indexOf(CLOSE, start + OPEN.length);
10
+ if (closeIndex === -1) return false;
11
+
12
+ if (silent) return true;
13
+
14
+ pushDelOpen(state, start + OPEN.length, closeIndex);
15
+ state.pos = closeIndex + CLOSE.length;
16
+ pushDelClose(state, start + OPEN.length, closeIndex);
17
+
18
+ return true;
19
+ }
20
+
21
+ export function pushDelOpen(state, contentStart, contentEnd) {
22
+ const isEmpty = state.src.slice(contentStart, contentEnd).trim() === "";
23
+ const token = state.push("critic_del_open", "del", 1);
24
+ token.meta = { empty: isEmpty };
25
+
26
+ if (!isEmpty) {
27
+ const maxPos = state.posMax;
28
+ state.pos = contentStart;
29
+ state.posMax = contentEnd;
30
+ state.md.inline.tokenize(state);
31
+ state.posMax = maxPos;
32
+ }
33
+ }
34
+
35
+ export function pushDelClose(state, contentStart, contentEnd) {
36
+ const isEmpty = state.src.slice(contentStart, contentEnd).trim() === "";
37
+ const token = state.push("critic_del_close", "del", -1);
38
+ token.meta = { empty: isEmpty };
39
+ }
40
+
41
+ export function renderDelOpen(tokens, idx) {
42
+ return tokens[idx].meta?.empty ? "<del>&nbsp;</del> " : "<del>";
43
+ }
44
+
45
+ export function renderDelClose(tokens, idx) {
46
+ return tokens[idx].meta?.empty ? "" : "</del>";
47
+ }
48
+
49
+ export function deletion(md) {
50
+ md.inline.ruler.push("critic_deletion", deletionRule);
51
+ md.renderer.rules.critic_del_open = renderDelOpen;
52
+ md.renderer.rules.critic_del_close = renderDelClose;
53
+ }
@@ -0,0 +1,42 @@
1
+ const OPEN = "{==";
2
+ const CLOSE = "==}";
3
+
4
+ function highlightRule(state, silent) {
5
+ const start = state.pos;
6
+
7
+ if (state.src.slice(start, start + OPEN.length) !== OPEN) return false;
8
+
9
+ const closeIndex = state.src.indexOf(CLOSE, start + OPEN.length);
10
+ if (closeIndex === -1) return false;
11
+
12
+ if (silent) return true;
13
+
14
+ const maxPos = state.posMax;
15
+
16
+ state.push("critic_mark_open", "mark", 1);
17
+
18
+ state.pos = start + OPEN.length;
19
+ state.posMax = closeIndex;
20
+ state.md.inline.tokenize(state);
21
+
22
+ state.pos = closeIndex + CLOSE.length;
23
+ state.posMax = maxPos;
24
+
25
+ state.push("critic_mark_close", "mark", -1);
26
+
27
+ return true;
28
+ }
29
+
30
+ function renderMarkOpen() {
31
+ return "<mark>";
32
+ }
33
+
34
+ function renderMarkClose() {
35
+ return "</mark>";
36
+ }
37
+
38
+ export function highlight(md) {
39
+ md.inline.ruler.push("critic_highlight", highlightRule);
40
+ md.renderer.rules.critic_mark_open = renderMarkOpen;
41
+ md.renderer.rules.critic_mark_close = renderMarkClose;
42
+ }
@@ -0,0 +1,54 @@
1
+ const ADDITION_OPEN = "{++";
2
+ const ADDITION_CLOSE = "++}";
3
+ const DELETION_OPEN = "{--";
4
+ const DELETION_CLOSE = "--}";
5
+
6
+ const ADDITION_BREAK_HTML = '<ins class="break">&nbsp;</ins>';
7
+ const DELETION_BREAK_HTML = "<del>&nbsp;</del> ";
8
+
9
+ function findDanglingSpan(tokens, i, openMarker, closeMarker) {
10
+ const isParagraphPair =
11
+ tokens[i]?.type === "paragraph_open" &&
12
+ tokens[i + 1]?.type === "inline" &&
13
+ tokens[i + 2]?.type === "paragraph_close" &&
14
+ tokens[i + 3]?.type === "paragraph_open" &&
15
+ tokens[i + 4]?.type === "inline" &&
16
+ tokens[i + 5]?.type === "paragraph_close";
17
+
18
+ if (!isParagraphPair) return null;
19
+
20
+ const before = tokens[i + 1].content.replace(/\s+$/, "");
21
+ const after = tokens[i + 4].content.replace(/^\s+/, "");
22
+
23
+ if (!before.endsWith(openMarker) || !after.startsWith(closeMarker)) return null;
24
+
25
+ return {
26
+ before: before.slice(0, -openMarker.length),
27
+ after: after.slice(closeMarker.length),
28
+ };
29
+ }
30
+
31
+ function unwrapParagraphBreaks(state) {
32
+ const tokens = state.tokens;
33
+
34
+ for (let i = 0; i < tokens.length - 5; i++) {
35
+ const addition = findDanglingSpan(tokens, i, ADDITION_OPEN, ADDITION_CLOSE);
36
+ const deletion = addition ? null : findDanglingSpan(tokens, i, DELETION_OPEN, DELETION_CLOSE);
37
+ const match = addition ?? deletion;
38
+
39
+ if (!match) continue;
40
+
41
+ tokens[i + 1].content = match.before;
42
+ tokens[i + 4].content = match.after;
43
+
44
+ const marker = new state.Token("html_block", "", 0);
45
+ marker.content = addition ? ADDITION_BREAK_HTML : DELETION_BREAK_HTML;
46
+ marker.block = true;
47
+
48
+ tokens.splice(i + 3, 0, marker);
49
+ }
50
+ }
51
+
52
+ export function paragraphBreak(md) {
53
+ md.core.ruler.after("block", "critic_paragraph_break", unwrapParagraphBreaks);
54
+ }
@@ -0,0 +1,41 @@
1
+ import { pushInsClose, pushInsOpen, renderInsClose, renderInsOpen } from "./addition.js";
2
+ import { pushDelClose, pushDelOpen, renderDelClose, renderDelOpen } from "./deletion.js";
3
+
4
+ const OPEN = "{~~";
5
+ const MID = "~>";
6
+ const CLOSE = "~~}";
7
+
8
+ function substitutionRule(state, silent) {
9
+ const start = state.pos;
10
+
11
+ if (state.src.slice(start, start + OPEN.length) !== OPEN) return false;
12
+
13
+ const midIndex = state.src.indexOf(MID, start + OPEN.length);
14
+ if (midIndex === -1) return false;
15
+
16
+ const closeIndex = state.src.indexOf(CLOSE, midIndex + MID.length);
17
+ if (closeIndex === -1) return false;
18
+
19
+ if (silent) return true;
20
+
21
+ const delContentStart = start + OPEN.length;
22
+ const insContentStart = midIndex + MID.length;
23
+
24
+ pushDelOpen(state, delContentStart, midIndex);
25
+ state.pos = insContentStart;
26
+ pushDelClose(state, delContentStart, midIndex);
27
+
28
+ pushInsOpen(state, insContentStart, closeIndex);
29
+ state.pos = closeIndex + CLOSE.length;
30
+ pushInsClose(state, insContentStart, closeIndex);
31
+
32
+ return true;
33
+ }
34
+
35
+ export function substitution(md) {
36
+ md.inline.ruler.push("critic_substitution", substitutionRule);
37
+ md.renderer.rules.critic_del_open = renderDelOpen;
38
+ md.renderer.rules.critic_del_close = renderDelClose;
39
+ md.renderer.rules.critic_ins_open = renderInsOpen;
40
+ md.renderer.rules.critic_ins_close = renderInsClose;
41
+ }
package/package.json ADDED
@@ -0,0 +1,25 @@
1
+ {
2
+ "name": "markdown-it-critic",
3
+ "version": "1.0.0",
4
+ "description": "CriticMarkup syntax as a markdown-it plugin",
5
+ "author": "Márton Visnovitz <vimtaai@pm.me>",
6
+ "license": "MIT",
7
+ "repository": "github:vimtaai/markdown-it-critic",
8
+ "type": "module",
9
+ "main": "lib/index.js",
10
+ "files": [
11
+ "lib/**/*.js",
12
+ "!lib/**/*.test.js"
13
+ ],
14
+ "scripts": {
15
+ "test": "node --test",
16
+ "test:watch": "node --watch --test",
17
+ "check": "biome check",
18
+ "check:ci": "biome ci",
19
+ "check:fix": "biome check --write"
20
+ },
21
+ "devDependencies": {
22
+ "@biomejs/biome": "^2.4.1",
23
+ "markdown-it": "^14.1.0"
24
+ }
25
+ }