-
-
Notifications
You must be signed in to change notification settings - Fork 382
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
156 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import type { Element, Root } from 'hast' | ||
import { toString } from 'hast-util-to-string' | ||
import type { RehypeShikiCoreOptions } from './types' | ||
|
||
interface InlineCodeProcessorContext { | ||
node: Element | ||
getLanguage: (lang?: string) => string | undefined | ||
highlight: ( | ||
lang: string, | ||
code: string, | ||
metaString?: string, | ||
meta?: Record<string, unknown> | ||
) => Root | undefined | ||
} | ||
|
||
type InlineCodeProcessor = (context: InlineCodeProcessorContext) => Root | undefined | ||
|
||
type Truthy<T> = T extends false | '' | 0 | null | undefined ? never : T | ||
|
||
export const InlineCodeProcessors: Record<Truthy<RehypeShikiCoreOptions['inline']>, InlineCodeProcessor> = { | ||
'tailing-curly-colon': ({ node, getLanguage, highlight }) => { | ||
const raw = toString(node) | ||
const match = raw.match(/(.+)\{:([\w-]+)\}$/) | ||
if (!match) | ||
return | ||
const lang = getLanguage(match[2]) | ||
if (!lang) | ||
return | ||
|
||
const code = match[1] ?? raw | ||
const fragment = highlight(lang, code) | ||
if (!fragment) | ||
return | ||
|
||
const head = fragment.children[0] | ||
if (head.type === 'element' && head.tagName === 'pre') { | ||
head.tagName = 'span' | ||
} | ||
|
||
return fragment | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import type { Element, Root } from 'hast' | ||
import type { | ||
BuiltinTheme, | ||
CodeOptionsMeta, | ||
CodeOptionsThemes, | ||
CodeToHastOptionsCommon, | ||
TransformerOptions, | ||
} from 'shiki' | ||
|
||
export interface MapLike<K = any, V = any> { | ||
get: (key: K) => V | undefined | ||
set: (key: K, value: V) => this | ||
} | ||
|
||
export interface RehypeShikiExtraOptions { | ||
/** | ||
* Add `language-*` class to code element | ||
* | ||
* @default false | ||
*/ | ||
addLanguageClass?: boolean | ||
|
||
/** | ||
* The default language to use when is not specified | ||
*/ | ||
defaultLanguage?: string | ||
|
||
/** | ||
* The fallback language to use when specified language is not loaded | ||
*/ | ||
fallbackLanguage?: string | ||
|
||
/** | ||
* `mdast-util-to-hast` adds a newline to the end of code blocks | ||
* | ||
* This option strips that newline from the code block | ||
* | ||
* @default true | ||
* @see https://github.com/syntax-tree/mdast-util-to-hast/blob/f511a93817b131fb73419bf7d24d73a5b8b0f0c2/lib/handlers/code.js#L22 | ||
*/ | ||
stripEndNewline?: boolean | ||
|
||
/** | ||
* Custom meta string parser | ||
* Return an object to merge with `meta` | ||
*/ | ||
parseMetaString?: ( | ||
metaString: string, | ||
node: Element, | ||
tree: Root | ||
) => Record<string, any> | undefined | null | ||
|
||
/** | ||
* Highlight inline code blocks | ||
* | ||
* - `false`: disable inline code block highlighting | ||
* - `tailing-curly-colon`: highlight with `\`code{:lang}\`` | ||
* | ||
* @see https://shiki.style/packages/rehype#inline-code | ||
* @default false | ||
*/ | ||
inline?: false | 'tailing-curly-colon' | ||
|
||
/** | ||
* Custom map to cache transformed codeToHast result | ||
* | ||
* @default undefined | ||
*/ | ||
cache?: MapLike<string, Root> | ||
|
||
/** | ||
* Chance to handle the error | ||
* If not provided, the error will be thrown | ||
*/ | ||
onError?: (error: unknown) => void | ||
} | ||
|
||
export type RehypeShikiCoreOptions = | ||
& CodeOptionsThemes<BuiltinTheme> | ||
& TransformerOptions | ||
& CodeOptionsMeta | ||
& RehypeShikiExtraOptions | ||
& Omit<CodeToHastOptionsCommon, 'lang'> |