Skip to content
This repository was archived by the owner on May 7, 2024. It is now read-only.

Commit d4859a9

Browse files
authored
avoid zero-length insert ops (#279)
fixes #276
1 parent fb10db7 commit d4859a9

File tree

5 files changed

+20
-5
lines changed

5 files changed

+20
-5
lines changed

.changeset/few-candles-care.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'grammarly-richtext-encoder': patch
3+
---
4+
5+
Fix markdown encoding when heading starts with a link

packages/grammarly-richtext-encoder/src/Language.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export function createTransformer(options: {
3535
}
3636

3737
function insert(text: string, node?: Parser.SyntaxNode, skip: number = 0): void {
38+
if (text === '') return
3839
richtext.ops.push({
3940
insert: text,
4041
attributes: text === '\n' ? pickBlockAttributes(attributes) : pickInlineAttributes(attributes),

packages/grammarly-richtext-encoder/src/LanguageMarkdown.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,13 @@ export const markdown = createTransformer({
132132
},
133133
processNode(node, insert) {
134134
if (node.type === 'text') {
135-
if (node.parent?.parent?.type === 'atx_heading') {
135+
if (node.parent?.parent?.type === 'atx_heading' && node.parent.firstChild?.equals(node)) {
136136
insert(node.text.slice(1), node, 1)
137-
} else if (node.parent?.type === 'paragraph' && node.text.startsWith(': ')) {
137+
} else if (
138+
node.parent?.type === 'paragraph' &&
139+
node.text.startsWith(': ') &&
140+
node.parent.firstChild?.equals(node)
141+
) {
138142
insert(node.text.slice(2), node, 2) // #255 - Definition Lists
139143
} else insert(node.text, node)
140144
} else if (node.type === 'line_break' || node.type === 'hard_line_break') {

packages/grammarly-richtext-encoder/test/__snapshots__/markdown.test.ts.snap

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,15 @@ exports[`markdown encode 1`] = `
5252
\\n
5353
First Term
5454
55-
This is the definition of the first term.
55+
: This is the definition of the first term.
5656
\\n
5757
Second Term
5858
59-
This is one definition of the second term.
59+
: This is one definition of the second term.
6060
61-
This is another definition of the second term.
61+
: This is another definition of the second term.
6262
\\n
63+
1 {"link":""}
64+
header
65+
\\n {"header":1}
6366
`;

packages/grammarly-richtext-encoder/test/markdown.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,5 @@ First Term
3838
Second Term
3939
: This is one definition of the second term.
4040
: This is another definition of the second term.
41+
42+
# [1] header

0 commit comments

Comments
 (0)