Skip to content

Commit

Permalink
Single line breaks don't end tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
thetarnav committed Feb 24, 2024
1 parent cf85e87 commit 0ffec07
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
8 changes: 4 additions & 4 deletions mds.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ export function write(s, chunk) {
const last_last_txt_char = s.txt[s.txt.length-2]
const last_last_src_char = s.src[s.idx-2]
const last_txt_char = s.txt[s.txt.length-1]
const last_src_char = s.src[s.idx-1]
const char = s.src[s.idx]
const in_token = s.tokens_type[s.tokens_len]

Expand Down Expand Up @@ -333,12 +334,11 @@ export function write(s, chunk) {
add_paragraph(s)
flush(s)
}
s.tokens_len = Math.min(s.tokens_len, 1)
continue
}
if ('\n' === s.src[s.idx-1]) {
if ('\n' === s.src[s.idx-2]) {
end_token(s)
if ('\n' === last_src_char) {
if ('\n' === last_last_src_char) {
s.tokens_len = 0
} else {
s.renderer.update_node(s.renderer.data, s.tokens_node[s.tokens_len], '\n')
}
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ mds.end(stream)

- [x] Paragraphs
- [x] Line breaks
- [ ] Line breaks don't end emphasis
- [x] Single line breaks don't end tokens
- [x] Headers
- [x] code block with triple backticks
- [x] `inline code` with backticks
Expand Down
21 changes: 21 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,27 @@ t.test("Line Breaks", () => {
assert.equal(renderer.data.temp_text_node, null)
})

t.test("Line Breaks with Italic", () => {
const renderer = test_renderer()
const parser = mds.parser(renderer)

mds.write(parser, "*" + content_1 + "\n" + content_2 + "*")
mds.end(parser)

assert.deepEqual(renderer.data.root, {
type : mds.Token_Type.Root,
children: [{
type : mds.Token_Type.Paragraph,
children: [{
type : mds.Token_Type.Italic_Ast,
children: [content_1, "\n", content_2]
}],
}]
})
assert.equal(renderer.data.temp_text, "")
assert.equal(renderer.data.temp_text_node, null)
})

t.test("Paragraphs", () => {
const renderer = test_renderer()
const parser = mds.parser(renderer)
Expand Down

0 comments on commit 0ffec07

Please sign in to comment.