Skip to content

Commit

Permalink
fix: support triple tick inline code, #35
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed Dec 15, 2024
1 parent 5c318cc commit 5746f01
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/nodes/inline-code.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NodeKind } from './node-kind'
import { Node } from './node'

export type InlineCodeDelimiter = '`' | '``'
export type InlineCodeDelimiter = '`' | '``' | '```'

export class InlineCode implements Node {
readonly children: Node[] = []
Expand Down
14 changes: 10 additions & 4 deletions src/parser/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ export function parse (str: string, options: NormalizedPadMarkdownOptions): Docu
codeLang = popMarkdown()
push(State.BlockCodeBody)
i++
} else if (state === State.BlockCodeLang && c3 === '```') {
const children = popMarkdown()
resolve(new InlineCode(children, c3))
i += 3
}
else if (state === State.BlockCodeBody && c3 === blockCodeDelimiter) {
resolve(new BlockCode(codeLang, blockCodeDelimiter, parseCode(popMarkdown(), codeLang, parse, options)))
Expand Down Expand Up @@ -277,13 +281,15 @@ export function parse (str: string, options: NormalizedPadMarkdownOptions): Docu
push(State.BlockCodeLang)
blockCodeDelimiter = c3
i += 3
}
else if (c3 === '---' && allowFrontMatter()) {
} else if (!blankLine && c3 === '```' && allow(NodeKind.InlineCode)) {
inlineCodeDelimiter = c3
push(State.InlineCode)
i += 3
} else if (c3 === '---' && allowFrontMatter()) {
push(State.BlockCodeLang)
blockCodeDelimiter = c3
i += 3
}
else if (c2 === '``' && allow(NodeKind.InlineCode)) {
} else if (c2 === '``' && allow(NodeKind.InlineCode)) {
inlineCodeDelimiter = c2
push(State.InlineCode)
i += 2
Expand Down
31 changes: 30 additions & 1 deletion test/parser/parse.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ describe('parse()', () => {
code: 'code'
})
})
it('should support doulbe `` inline code', () => {
it('should support double `` inline code', () => {
const doc = parse('``code``', options)
expect(doc.children).toHaveLength(1)
expect(doc.children[0]).toMatchObject({
Expand Down Expand Up @@ -329,6 +329,35 @@ describe('parse()', () => {
code: 'code'
})
})
it('should parse ```triple tick``` inline code', () => {
const doc = parse('```code```', options)
expect(doc.children).toHaveLength(1)
expect(doc.children[0]).toMatchObject({
kind: NodeKind.InlineCode,
code: 'code'
})
})
it('should parse ```triple tick``` inline code in context', () => {
const doc = parse('bar ```code``` foo', options)
expect(doc.children).toMatchObject([{
children: [],
kind: NodeKind.AlphabetNumeric,
text: 'bar'
}, {
kind: NodeKind.Blank,
char: ' '
}, {
kind: NodeKind.InlineCode,
code: 'code'
}, {
kind: NodeKind.Blank,
char: ' '
}, {
children: [],
kind: NodeKind.AlphabetNumeric,
text: 'foo'
}])
})
})

describe('Emphasis', () => {
Expand Down
8 changes: 6 additions & 2 deletions test/transformers/pad-markdown.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { padMarkdown } from '../../src/transformers/pad-markdown'

describe('padding()', () => {
describe('code', () => {
it('should pad between code fence', () => {
describe('inline code', () => {
it('should pad between inline code', () => {
expect(padMarkdown('file`/foo.txt`not exists'))
.toEqual('file `/foo.txt` not exists')
})
it('should pad between triple tick inline code', () => {
expect(padMarkdown('```foo```\nfile```/foo.txt```not exists'))
.toEqual('```foo```\nfile ```/foo.txt``` not exists')
})
it('should ignore padded code fence', () => {
expect(padMarkdown('file `/foo.txt` not exists'))
.toEqual('file `/foo.txt` not exists')
Expand Down

0 comments on commit 5746f01

Please sign in to comment.