Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
shiki-twoslash
Advanced tools
Documentation / made lovely by counting words / maybe we would read!
Provides the API primitives to mix shiki with @typescript/twoslash to provide rich contextual code samples.
Things it handles:
createShikiHighlighter
runTwoSlash
renderCodeToHTML
The config which a user passes is an intersection of Shiki's HighlighterOptions
interface HighlighterOptions {
theme?: IThemeRegistration
langs?: (Lang | ILanguageRegistration)[]
themes?: IThemeRegistration[]
/**
* Paths for loading themes and langs. Relative to the package's root.
*/
paths?: IHighlighterPaths
}
With twoslash's TwoSlashOptions
export interface TwoSlashOptions {
/** Allows setting any of the handbook options from outside the function, useful if you don't want LSP identifiers */
defaultOptions?: Partial<ExampleOptions>
/** Allows setting any of the compiler options from outside the function */
defaultCompilerOptions?: CompilerOptions
/** Allows applying custom transformers to the emit result, only useful with the showEmit output */
customTransformers?: CustomTransformers
/** An optional copy of the TypeScript import, if missing it will be require'd. */
tsModule?: TS
/** An optional copy of the lz-string import, if missing it will be require'd. */
lzstringModule?: LZ
/**
* An optional Map object which is passed into @typescript/vfs - if you are using twoslash on the
* web then you'll need this to set up your lib *.d.ts files. If missing, it will use your fs.
*/
fsMap?: Map<string, string>
/** The cwd for the folder which the virtual fs should be overlaid on top of when using local fs, opts to process.cwd() if not present */
vfsRoot?: string
}
Most people will want to set a theme
, and maybe vfsRoot
if they want to do twoslash with custom libraries in a monorepo:
{
resolve: "gatsby-remark-shiki-twoslash",
options: {
theme: "github-light",
vfsRoot: path.join(__dirname, "..", "..")
},
}
To get the Node globals set up, import them via an inline triple-slash reference:
```ts twoslash
/// <reference types="node" />
import { execSync } from "child_process"
const files = execSync("git status --porcelain", { encoding: "utf8" })
files.length
```
This applies to other projects which use globals, like Jest etc. If you think that's ugly, that's OK, you can use // ---cut---
to trim the user-visible output.
The user-exposed parts of the API is a single file, you might find it easier to just read that: src/index.ts
.
createShikiHighlighter
Sets up the highlighter for Shiki, accepts shiki options:
async function visitor(highlighterOpts) {
const highlighter = await createShikiHighlighter(userOpts)
visit(markdownAST, "code", visitor(highlighter, userOpts))
}
renderCodeToHTML
/**
* Renders a code sample to HTML, automatically taking into account:
*
* - rendering overrides for twoslash and tsconfig
* - whether the language exists in shiki
*
* @param code the source code to render
* @param lang the language to use in highlighting
* @param info additional metadata which lives after the codefence lang (e.g. ["twoslash"])
* @param highlighter optional, but you should use it, highlighter
* @param twoslash optional, but required when info contains 'twoslash' as a string
*/
export declare const renderCodeToHTML: (
code: string,
lang: string,
info: string[],
shikiOptions?: import("shiki/dist/renderer").HtmlRendererOptions | undefined,
highlighter?: Highlighter | undefined,
twoslash?: TwoSlashReturn | undefined
) => string
For example:
const results = renderCodeToHTML(node.value, lang, node.meta || [], {}, highlighter, node.twoslash)
node.type = "html"
node.value = results
node.children = []
Uses:
renderers.plainTextRenderer
for language which shiki cannot handlerenderers.defaultRenderer
for shiki highlighted code samplesrenderers.twoslashRenderer
for twoslash powered TypeScript code samplesrenderers.tsconfigJSONRenderer
for extra annotations to JSON which is known to be a TSConfig fileThese will be used automatically for you, depending on whether the language is available or what the info
param is set to.
To get access to the twoslash renderer, you'll need to pass in the results of a twoslash run to renderCodeToHTML
:
const highlighter = await createShikiHighlighter(highlighterOpts)
const twoslashResults = runTwoSlash(code, lang)
const results = renderCodeToHTML(
twoslashResults.code,
twoslashResults.lang,
node.meta || ["twoslash"],
{},
highlighter,
node.twoslash
)
runTwoSlash
Used to run Twoslash on a code sample. In this case it's looking at a code AST node and switching out the HTML with the twoslash results:
if (node.meta && node.meta.includes("twoslash")) {
const results = runTwoSlash(node.value, node.lang, settings)
node.value = results.code
node.lang = results.extension
node.twoslash = results
}
FAQs
API primitives to mix Shiki with Twoslash
We found that shiki-twoslash demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.