Skip to content

Commit

Permalink
fix: remove trailing newline added by markdown-it (#585)
Browse files Browse the repository at this point in the history
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
  • Loading branch information
KermanX and antfu authored Feb 12, 2024
1 parent 2f64ffd commit b559cde
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
16 changes: 16 additions & 0 deletions packages/markdown-it/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ export interface MarkdownItShikiExtraOptions {
code: string,
lang: string,
) => Record<string, any> | undefined | null

/**
* markdown-it's highlight function will add a trailing newline to the code.
*
* This integration removes the trailing newline to the code by default,
* you can turn this off by passing false.
*
* @default true
*/
trimEndingNewline?: boolean
}

export type MarkdownItShikiSetupOptions =
Expand All @@ -26,6 +36,7 @@ export function setupMarkdownIt(
) {
const {
parseMetaString,
trimEndingNewline = true,
} = options

markdownit.options.highlight = (code, lang = 'text', attrs) => {
Expand All @@ -49,6 +60,11 @@ export function setupMarkdownIt(
},
})

if (trimEndingNewline) {
if (code.endsWith('\n'))
code = code.slice(0, -1)
}

return highlighter.codeToHtml(
code,
{
Expand Down
3 changes: 1 addition & 2 deletions packages/markdown-it/test/fixtures/a.out.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions packages/twoslash/test/out/markdown-it/works.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b559cde

Please sign in to comment.