Skip to content

Commit

Permalink
feat(markdown): support single line code import syntax (close #1355)
Browse files Browse the repository at this point in the history
  • Loading branch information
meteorlxy committed Jun 19, 2024
1 parent 41ad211 commit 34e291e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@ const MIN_LENGTH = 9
const START_CODES = [64, 91, 99, 111, 100, 101]

// regexp to match the import syntax
const SYNTAX_RE = /^@\[code(?:{(?:(\d+)?-(\d+)?)})?(?: ([^\]]+))?\]\(([^)]*)\)/
const SYNTAX_RE =
/^@\[code(?:{(?:(?:(?<lineStart>\d+)?-(?<lineEnd>\d+)?)|(?<lineSingle>\d+))})?(?: (?<info>[^\]]+))?\]\((?<importPath>[^)]*)\)/

/**
* Utility function to parse line number from line string that matched by SYNTAX_RE
*/
const parseLineNumber = (line: string | undefined): number | undefined =>
line ? Number.parseInt(line, 10) : undefined

export const createImportCodeBlockRule =
({ handleImportPath = (str) => str }: ImportCodePluginOptions): RuleBlock =>
Expand All @@ -36,17 +43,21 @@ export const createImportCodeBlockRule =

// check if it's matched the syntax
const match = state.src.slice(pos, max).match(SYNTAX_RE)
if (!match) return false
if (!match?.groups) return false

// return true as we have matched the syntax
if (silent) return true

const [, lineStart, lineEnd, info, importPath] = match
const { info, importPath } = match.groups

const lineSingle = parseLineNumber(match.groups.lineSingle)
const lineStart = lineSingle ?? parseLineNumber(match.groups.lineStart) ?? 0
const lineEnd = lineSingle ?? parseLineNumber(match.groups.lineEnd)

const meta: ImportCodeTokenMeta = {
importPath: handleImportPath(importPath),
lineStart: lineStart ? Number.parseInt(lineStart, 10) : 0,
lineEnd: lineEnd ? Number.parseInt(lineEnd, 10) : undefined,
lineStart,
lineEnd,
}

// create a import_code token
Expand Down
10 changes: 10 additions & 0 deletions packages/markdown/tests/plugins/importCodePlugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,10 @@ ${mdFixtureContent}\
const source = `\
@[code{1-2}](${jsFixturePathRelative})
@[code{1-}](${jsFixturePathRelative})
@[code{2}](${jsFixturePathRelative})
@[code{4-5}](${mdFixturePathRelative})
@[code{-5}](${mdFixturePathRelative})
@[code{5}](${mdFixturePathRelative})
`

const expected = `\
Expand All @@ -98,12 +100,18 @@ ${jsFixtureContent.split('\n').slice(0, 2).join('\n').replace(/\n?$/, '\n')}\
<pre><code class="language-js">\
${jsFixtureContent.split('\n').slice(0).join('\n').replace(/\n?$/, '\n')}\
</code></pre>
<pre><code class="language-js">\
${jsFixtureContent.split('\n').slice(1, 1).join('\n').replace(/\n?$/, '\n')}\
</code></pre>
<pre><code class="language-md">\
${mdFixtureContent.split('\n').slice(3, 5).join('\n').replace(/\n?$/, '\n')}\
</code></pre>
<pre><code class="language-md">\
${mdFixtureContent.split('\n').slice(0, 5).join('\n').replace(/\n?$/, '\n')}\
</code></pre>
<pre><code class="language-md">\
${mdFixtureContent.split('\n').slice(4, 5).join('\n').replace(/\n?$/, '\n')}\
</code></pre>
`

const md = MarkdownIt().use(importCodePlugin)
Expand All @@ -116,6 +124,8 @@ ${mdFixtureContent.split('\n').slice(0, 5).join('\n').replace(/\n?$/, '\n')}\
expect(env.importedFiles).toEqual([
jsFixturePath,
jsFixturePath,
jsFixturePath,
mdFixturePath,
mdFixturePath,
mdFixturePath,
])
Expand Down

0 comments on commit 34e291e

Please sign in to comment.