Skip to content

Commit

Permalink
Merge 3d3ad95 into b750079
Browse files Browse the repository at this point in the history
  • Loading branch information
obgnail committed Mar 9, 2024
2 parents b750079 + 3d3ad95 commit 9696128
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 24 deletions.
4 changes: 3 additions & 1 deletion src/parser/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ export function parse (str: string, options :NormalizedPadMarkdownOptions): Docu
push(State.HTMLTag)
i++
}
else if (c3 === '```' && allow(NodeKind.BlockCode)) {
else if (blankLine && c3 === '```') {
push(State.BlockCodeLang)
blockCodeDelimiter = c3
i += 3
Expand Down Expand Up @@ -268,10 +268,12 @@ export function parse (str: string, options :NormalizedPadMarkdownOptions): Docu
push(State.UnorderedListItem)
listPrefix = c2
i += 2
continue
} else if (blankLine && OrderedListItem.isValidPrefix(c3) && allow(NodeKind.OrderedListItem)) {
push(State.OrderedListItem)
listPrefix = c3
i += 3
continue
} else if (blankLine && BlockquoteItem.isValidPrefix(c2) && allow(NodeKind.BlockquoteItem)) {
push(State.BlockquoteItem)
i++
Expand Down
85 changes: 62 additions & 23 deletions test/parser/parse.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -581,31 +581,30 @@ describe('parse()', () => {
expect(blockCode.getCode()).toEqual('code\n')
})
it('should parse mixed text and code', () => {
const doc = parse('A`inline code`B```\nblock code\n```C', options)
expect(doc.children).toHaveLength(5)

expect(doc.children[0]).toMatchObject({
kind: NodeKind.AlphabetNumeric,
text: 'A'
})
expect(doc.children[1]).toMatchObject({
kind: NodeKind.InlineCode,
code: 'inline code'
})
expect(doc.children[2]).toMatchObject({
kind: NodeKind.AlphabetNumeric,
text: 'B'
})
const blockCode = doc.children[3] as BlockCode
expect(blockCode).toMatchObject({
kind: NodeKind.BlockCode,
lang: ''
const doc = parse('A`inline code`B\n```\nblock code\n```C', options)
expect(doc).toMatchObject({
children: [{
kind: NodeKind.AlphabetNumeric,
text: 'A'
}, {
kind: NodeKind.InlineCode,
code: 'inline code'
}, {
kind: NodeKind.AlphabetNumeric,
text: 'B'
}, {
kind: NodeKind.Blank,
char: '\n'
}, {
kind: NodeKind.BlockCode,
lang: ''
}, {
kind: NodeKind.AlphabetNumeric,
text: 'C'
}]
})
const blockCode = doc.children[4] as BlockCode
expect(blockCode.getCode()).toEqual('block code\n')
expect(doc.children[4]).toMatchObject({
kind: NodeKind.AlphabetNumeric,
text: 'C'
})
})
it('when there is front matter, the delimiter of the code block should be ``` instead of ---', () => {
const doc = parse('---\ntest:1\n---\n```js\nconsole.log(1)\n```', options)
Expand Down Expand Up @@ -721,6 +720,46 @@ describe('parse()', () => {
})
expect(item2.toMarkdown()).toEqual('*\tbar')
})

it('should recognize fenced code block in UnorderedListItem', () => {
const doc = parse('- ```js\n alert("123")\n ```\n- 前word后', options)
expect(doc.children).toHaveLength(3)

const [item1, blank, item2] = doc.children
expect(blank).toMatchObject({
kind: NodeKind.Blank,
char: '\n'
})

expect(item1).toMatchObject({
kind: NodeKind.UnorderedListItem,
prefix: '- '
})
expect(item1.children).toHaveLength(1)
expect(item1.children[0]).toMatchObject({
kind: NodeKind.BlockCode,
lang: 'js'
})

expect(item2).toMatchObject({
kind: NodeKind.UnorderedListItem,
prefix: '- '
})
expect(item2.children).toHaveLength(3)
const [u1, a1, u2] = item2.children
expect(u1).toMatchObject({
kind: NodeKind.UnicodeString,
text: '前'
})
expect(a1).toMatchObject({
kind: NodeKind.AlphabetNumeric,
text: 'word'
})
expect(u2).toMatchObject({
kind: NodeKind.UnicodeString,
text: '后'
})
})
})

describe('Blockquote', () => {
Expand Down

0 comments on commit 9696128

Please sign in to comment.