Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(shiki): highlight excerpt #1802

Merged
merged 1 commit into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
68 changes: 42 additions & 26 deletions src/runtime/transformers/shiki/shiki.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { visit } from 'unist-util-visit'
import type { MarkdownRoot } from '../../types'
import { defineTransformer } from '../utils'
import { useShikiHighlighter } from './highlighter'
import type { TokenColorMap, MarkdownNode } from './types'
Expand All @@ -8,39 +9,54 @@ export default defineTransformer({
extensions: ['.md'],
transform: async (content, options = {}) => {
const shikiHighlighter = useShikiHighlighter(options)
const colorMap: TokenColorMap = {}
const codeBlocks: any[] = []
const inlineCodes: any = []
visit(
content.body,
(node: any) => (node.tag === 'code' && node?.props.code) || (node.tag === 'code-inline' && (node.props?.lang || node.props?.language)),
(node) => {
if (node.tag === 'code') {
codeBlocks.push(node)
} else if (node.tag === 'code-inline') {
inlineCodes.push(node)
}

await Promise.all([
highlight(content.body),
highlight(content.excerpt)
])

return content

/**
* Highlight document with code nodes
* @param document tree
*/
async function highlight (document: MarkdownRoot) {
if (!document) {
return
}
)
const colorMap: TokenColorMap = {}
const codeBlocks: any[] = []
const inlineCodes: any = []
visit(
document,
(node: any) => (node?.tag === 'code' && node?.props.code) || (node?.tag === 'code-inline' && (node.props?.lang || node.props?.language)),
(node: MarkdownNode) => {
if (node?.tag === 'code') {
codeBlocks.push(node)
} else if (node?.tag === 'code-inline') {
inlineCodes.push(node)
}
}
)

await Promise.all(codeBlocks.map(highlightBlock))
await Promise.all(inlineCodes.map(highlightInline))
await Promise.all(codeBlocks.map((node: MarkdownNode) => highlightBlock(node, colorMap)))
await Promise.all(inlineCodes.map((node: MarkdownNode) => highlightInline(node, colorMap)))

// Inject token colors at the end of the document
if (Object.values(colorMap).length) {
content.body.children.push({
type: 'element',
tag: 'style',
children: [{ type: 'text', value: shikiHighlighter.generateStyles(colorMap) }]
})
// Inject token colors at the end of the document
if (Object.values(colorMap).length) {
document?.children.push({
type: 'element',
tag: 'style',
children: [{ type: 'text', value: shikiHighlighter.generateStyles(colorMap) }]
})
}
}

return content

/**
* Highlight inline code
*/
async function highlightInline (node: MarkdownNode) {
async function highlightInline (node: MarkdownNode, colorMap: TokenColorMap) {
const code = node.children![0].value!

// Fetch highlighted tokens
Expand All @@ -56,7 +72,7 @@ export default defineTransformer({
/**
* Highlight a code block
*/
async function highlightBlock (node: MarkdownNode) {
async function highlightBlock (node: MarkdownNode, colorMap: TokenColorMap) {
const { code, language: lang, highlights = [] } = node.props!

const innerCodeNode = node.children![0].children![0]
Expand Down
21 changes: 21 additions & 0 deletions test/features/highlighter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,5 +160,26 @@ export const testHighlighter = () => {
]
`)
})

test('highlight excerpt', async () => {
const parsed = await $fetch('/api/parse', {
method: 'POST',
body: {
id: 'content:index.md',
content: [
'```ts',
'const a: number = 1',
'```',
'<!--more-->',
'Second block'
].join('\n')
}
})

const styleExcerpt = parsed.excerpt.children.pop()
expect(styleExcerpt.tag).toBe('style')
const styleBody = parsed.body.children.pop()
expect(styleBody.tag).toBe('style')
})
})
}