Skip to content

Commit

Permalink
Merge abb218e into 40058bf
Browse files Browse the repository at this point in the history
  • Loading branch information
obgnail committed Jan 7, 2024
2 parents 40058bf + abb218e commit 80bb226
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/nodes/blockquote-item.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { NodeKind } from './node-kind'
import { Node } from './node'
import { isInlineBlank } from '../utils/char'

export type BlockquotePrefix = '> '

export class BlockquoteItem implements Node {
readonly children: Node[] = []
readonly prefix: string
readonly kind = NodeKind.BlockquoteItem

constructor (prefix: string, children: Node[]) {
this.prefix = prefix
this.children = children
}

toMarkdown () {
return this.prefix + this.children.map(x => x.toMarkdown()).join('')
}

static isValidPrefix (str: string) {
return str[0] === '>' && isInlineBlank(str[1])
}
}
1 change: 1 addition & 0 deletions src/nodes/node-kind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const enum NodeKind {
ReferenceImage = 1 << 19,
Raw = 1 << 20,
Math = 1 << 21,
BlockquoteItem = 1 << 22,

// combinations
All = -1,
Expand Down
13 changes: 13 additions & 0 deletions src/parser/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { Blank } from '../nodes/blank'
import { Raw } from '../nodes/raw'
import { OrderedListItem } from '../nodes/ordered-list-item'
import { UnorderedListItem } from '../nodes/unordered-list-item'
import { BlockquoteItem, BlockquotePrefix } from '../nodes/blockquote-item'
import { State } from './state'
import { Stack } from '../utils/stack'
import { Mask } from './mask'
Expand All @@ -42,6 +43,7 @@ export function parse (str: string, options :NormalizedPadMarkdownOptions): Docu
let blankLine = true
let listPrefix = ''
let codeLang = ''
const blockquotePrefix: BlockquotePrefix = '> '
let strongDelimiter: StrongDelimiter = '**'
let emphasisDelimiter: EmphasisDelimiter = '*'
let inlineCodeDelimiter: InlineCodeDelimiter = '`'
Expand Down Expand Up @@ -196,6 +198,10 @@ export function parse (str: string, options :NormalizedPadMarkdownOptions): Docu
resolve(new OrderedListItem(listPrefix, popNodes()), new Blank(c))
i++
}
else if (state === State.BlockquoteItem && c === '\n') {
resolve(new BlockquoteItem(blockquotePrefix, popNodes()), new Blank(c))
i++
}

// Quoted
else if (state === State.Quoted && c === '"') {
Expand Down Expand Up @@ -251,6 +257,10 @@ export function parse (str: string, options :NormalizedPadMarkdownOptions): Docu
push(State.OrderedListItem)
listPrefix = c3
i += 3
} else if (blankLine && BlockquoteItem.isValidPrefix(c2) && allow(NodeKind.BlockquoteItem)) {
push(State.BlockquoteItem)
i += 2
continue
} else if (c2 === '~~' && allow(NodeKind.Strikethrough)) {
push(State.Strikethrough)
i += 2
Expand Down Expand Up @@ -389,6 +399,9 @@ export function parse (str: string, options :NormalizedPadMarkdownOptions): Docu
case State.UnorderedListItem:
resolve(new UnorderedListItem(listPrefix, popNodes()))
break
case State.BlockquoteItem:
resolve(new BlockquoteItem(blockquotePrefix, popNodes()))
break
case State.BlockCodeBody:
resolve(new BlockCode(codeLang, blockCodeDelimiter, parseCode(popMarkdown(), codeLang, parse, options), false))
break
Expand Down
1 change: 1 addition & 0 deletions src/parser/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export enum State {
BlockCodeBody, // ```cpp\n
OrderedListItem, // 1.<space>
UnorderedListItem, // *<space>
BlockquoteItem, // ><space>

// natural language syntax
Quoted, // " in natural language
Expand Down
50 changes: 50 additions & 0 deletions test/parser/parse.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,56 @@ describe('parse()', () => {
})
expect(item2.toMarkdown()).toEqual('*\tbar')
})
it('should recognize UnorderedListItem with * prefix in blockquote', () => {
const doc = parse('> * **测试**', options)
expect(doc.children).toHaveLength(1)
const [blockquote] = doc.children
expect(blockquote).toMatchObject({
kind: NodeKind.BlockquoteItem,
prefix: '> '
})
const [unorderedList] = blockquote.children
expect(unorderedList.children).toHaveLength(1)
expect(unorderedList).toMatchObject({
kind: NodeKind.UnorderedListItem,
prefix: '* '
})
const [strong] = unorderedList.children
expect(strong.children).toHaveLength(1)
expect(strong).toMatchObject({
kind: NodeKind.Strong,
prefix: '**'
})
expect(strong.toMarkdown()).toEqual('**测试**')
})
it('should recognize UnorderedListItem with * prefix in nested blockquote', () => {
const doc = parse('> > * *测试*', options)
expect(doc.children).toHaveLength(1)
const [blockquote] = doc.children
expect(blockquote).toMatchObject({
kind: NodeKind.BlockquoteItem,
prefix: '> '
})
const [blockquote2] = blockquote.children
expect(blockquote2.children).toHaveLength(1)
expect(blockquote2).toMatchObject({
kind: NodeKind.BlockquoteItem,
prefix: '> '
})
const [unorderedList] = blockquote2.children
expect(unorderedList.children).toHaveLength(1)
expect(unorderedList).toMatchObject({
kind: NodeKind.UnorderedListItem,
prefix: '* '
})
const [emphasis] = unorderedList.children
expect(emphasis.children).toHaveLength(1)
expect(emphasis).toMatchObject({
kind: NodeKind.Emphasis,
prefix: '*'
})
expect(emphasis.toMarkdown()).toEqual('*测试*')
})
})

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

0 comments on commit 80bb226

Please sign in to comment.