Skip to content

Commit

Permalink
Update GeneratorOptions shiki object type
Browse files Browse the repository at this point in the history
- Use createHighlighter instead of deprecated getHighlighter
- Allow dual theme field
- Mark transformers as optional
  • Loading branch information
Trinovantes committed Aug 14, 2024
1 parent bbced59 commit fa16f5f
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 30 deletions.
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,10 @@ import { RstToHtmlCompiler, RstGeneratorOptions } from 'rst-compiler'

const generatorOptions: Partial<RstGeneratorOptions> = {
shiki: {
defaultLanguage: 'python',
theme: 'min-dark',
transformers: [],
highlighter: await getHighlighter({
langs: ['py', 'js', 'cpp'],
themes: ['min-dark'],
theme: 'github-light',
highlighter: await createHighlighter({
langs: ['python', 'js'],
themes: ['github-light'],
}),
},
}
Expand Down
5 changes: 2 additions & 3 deletions demo/components/CodePreview.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script lang="ts" setup>
import { ref, watch } from 'vue'
import { codeToHtml, getHighlighter } from 'shiki'
import { codeToHtml, createHighlighter } from 'shiki'
import { useQuasar } from 'quasar'
import { RstGeneratorOptions, RstParserOptions, RstToHtmlCompiler, RstToMdCompiler } from '@/index.js'
Expand Down Expand Up @@ -70,8 +70,7 @@ watch(editorText, async(editorText) => {
const generatorOptions: Partial<RstGeneratorOptions> = {
shiki: {
theme: 'github-light',
transformers: [],
highlighter: await getHighlighter({
highlighter: await createHighlighter({
langs: ['python', 'js'],
themes: ['github-light'],
}),
Expand Down
10 changes: 4 additions & 6 deletions demo/components/DefaultText.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,10 @@ However, this demo is configured to use :code:`shiki` for syntax highlighting:
const generatorOptions: Partial<RstGeneratorOptions> = {
shiki: {
defaultLanguage: 'python',
theme: 'min-dark',
transformers: [],
highlighter: await getHighlighter({
langs: ['py', 'js', 'cpp'],
themes: ['min-dark'],
theme: 'github-light',
highlighter: await createHighlighter({
langs: ['python', 'js'],
themes: ['github-light'],
}),
},
}
Expand Down
8 changes: 4 additions & 4 deletions src/Generator/RstGeneratorOptions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import merge from 'lodash.merge'
import { KatexOptions } from 'katex'
import { ShikiTransformer, getHighlighter } from 'shiki'
import { createHighlighter, ShikiTransformer, CodeOptionsMultipleThemes } from 'shiki'

export type RstGeneratorOptions = {
disableWarnings: boolean
Expand Down Expand Up @@ -106,9 +106,9 @@ export type RstGeneratorOptions = {
* If not set, code blocks will output as `<pre>` and inline code will output as `<code>`
*/
shiki?: {
theme: string
transformers: Array<ShikiTransformer>
highlighter: Awaited<ReturnType<typeof getHighlighter>>
highlighter: Awaited<ReturnType<typeof createHighlighter>>
theme: string | CodeOptionsMultipleThemes['themes']
transformers?: Array<ShikiTransformer>
}

/**
Expand Down
20 changes: 15 additions & 5 deletions src/Plugins/Code/renderCodeBlockHtml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,21 @@ export function renderCodeBlockHtml(generatorState: RstGeneratorState, language:
return
}

const renderedCode = generatorState.opts.shiki.highlighter.codeToHtml(rawCode, {
lang: language,
theme: generatorState.opts.shiki.theme,
transformers: generatorState.opts.shiki.transformers,
})
let renderedCode: string

if (typeof generatorState.opts.shiki.theme === 'string') {
renderedCode = generatorState.opts.shiki.highlighter.codeToHtml(rawCode, {
lang: language,
theme: generatorState.opts.shiki.theme,
transformers: generatorState.opts.shiki.transformers,
})
} else {
renderedCode = generatorState.opts.shiki.highlighter.codeToHtml(rawCode, {
lang: language,
themes: generatorState.opts.shiki.theme,
transformers: generatorState.opts.shiki.transformers,
})
}

generatorState.writeText(renderedCode)
})
Expand Down
11 changes: 5 additions & 6 deletions tests/playground/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { RstToHtmlCompiler } from '@/RstCompiler.js'
import fs from 'node:fs'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import { getHighlighter } from 'shiki'
import { createHighlighter } from 'shiki'

const port = 8080

Expand All @@ -16,11 +16,10 @@ Bun.serve({
const { body, header } = new RstToHtmlCompiler().compile(rst, {}, {
defaultSyntaxLanguage: 'python',
shiki: {
theme: 'min-dark',
transformers: [],
highlighter: await getHighlighter({
langs: ['py', 'js', 'cpp', 'csharp'],
themes: ['min-dark'],
theme: 'github-light',
highlighter: await createHighlighter({
langs: ['python', 'js'],
themes: ['github-light'],
}),
},
})
Expand Down

0 comments on commit fa16f5f

Please sign in to comment.