Skip to content

Commit

Permalink
Properly track textblock start when serializing
Browse files Browse the repository at this point in the history
FIX: Fix a bug that caused the serializer to not escape start-of-line
markup when inside a list.

Closes #75
  • Loading branch information
marijnh committed Jul 4, 2022
1 parent 847322b commit 0a7a131
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/to_markdown.ts
Expand Up @@ -163,6 +163,8 @@ export class MarkdownSerializerState {
/// @internal
inAutolink: boolean | undefined = undefined
/// @internal
atBlockStart: boolean = false
/// @internal
inTightList: boolean = false

/// @internal
Expand Down Expand Up @@ -236,9 +238,8 @@ export class MarkdownSerializerState {
text(text: string, escape = true) {
let lines = text.split("\n")
for (let i = 0; i < lines.length; i++) {
var startOfLine = this.atBlank() || this.closed
this.write()
this.out += escape ? this.esc(lines[i], !!startOfLine) : lines[i]
this.out += escape ? this.esc(lines[i], this.atBlockStart) : lines[i]
if (i != lines.length - 1) this.out += "\n"
}
}
Expand All @@ -257,6 +258,7 @@ export class MarkdownSerializerState {

/// Render the contents of `parent` as inline content.
renderInline(parent: Node) {
this.atBlockStart = true
let active: Mark[] = [], trailing = ""
let progress = (node: Node | null, offset: number, index: number) => {
let marks = node ? node.marks : []
Expand Down Expand Up @@ -344,6 +346,7 @@ export class MarkdownSerializerState {
}
parent.forEach(progress)
progress(null, 0, parent.childCount)
this.atBlockStart = false
}

/// Render a node's content as a list. `delim` should be the extra
Expand Down Expand Up @@ -381,7 +384,7 @@ export class MarkdownSerializerState {

/// @internal
quote(str: string) {
var wrap = str.indexOf('"') == -1 ? '""' : str.indexOf("'") == -1 ? "''" : "()"
let wrap = str.indexOf('"') == -1 ? '""' : str.indexOf("'") == -1 ? "''" : "()"
return wrap[0] + str + wrap[1]
}

Expand Down
4 changes: 4 additions & 0 deletions test/test-parse.ts
Expand Up @@ -183,4 +183,8 @@ describe("markdown", () => {
{escapeExtraCharacters: /[\|!]/g})
ist(markdownSerializer.serialize(doc(p("foo|bar!"))), "foo\\|bar\\!")
})

it("escapes list markers inside lists", () => {
same("* 1\\. hi\n\n* x", doc(ul(li(p("1. hi")), li(p("x")))))
})
})

0 comments on commit 0a7a131

Please sign in to comment.