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

Allow style attributes in fence meta #185

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions packages/shiki-twoslash/src/renderers/plain.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { escapeHtml, Meta } from "../utils"
import { stripHTML, escapeHtml, Meta } from "../utils"

// C&P'd from shiki
export interface HtmlRendererOptions {
Expand All @@ -20,23 +20,34 @@ export const preOpenerFromRenderingOptsWithExtras = (opts: HtmlRendererOptions,
.join(" ")
.trim()

const attributes = Object.entries(meta)
const style = [`background-color: ${bg}; color: ${fg}`, meta.style ?? ""]
.filter(Boolean)
.join("; ")
.trim()

const attributes = {
...meta,
class: classList,
style,
}

const attributesString = Object.entries(attributes)
.filter(entry => {
// exclude types other than string, number, boolean
// exclude keys class, twoslash
// exclude key twoslash
// exclude falsy booleans
return (
["string", "number", "boolean"].includes(typeof entry[1]) &&
!["class", "twoslash"].includes(entry[0]) &&
!["twoslash"].includes(entry[0]) &&
entry[1] !== false
)
})
.map(([key, value]) => `${key}="${value}"`)
.map(([key, value]) => `${key}="${stripHTML((value as string).toString())}"`)
.join(" ")
.trim()

// prettier-ignore
return `<pre class="${classList}" style="background-color: ${bg}; color: ${fg}"${attributes ? ` ${attributes}`: ''}>`
return `<pre ${attributesString}>`
}

/** You don't have a language which shiki twoslash can handle, make a DOM compatible version */
Expand Down
12 changes: 12 additions & 0 deletions packages/shiki-twoslash/test/twoslash-renderer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,3 +313,15 @@ const a = 123
})


it("includes extra attributes on the pre", async () => {
const highlighter = await createShikiHighlighter({ theme: "nord" })
const meta = {
title: "Hello",
class: "rotated shadow",
style: "--agent: smith;",
onclick: "return doTheThing(\"now\") && succeed()",
}
const html = renderCodeToHTML( "", "ts", meta, { themeName: "nord" }, highlighter)

expect(html).toContain(`<pre title="Hello" class="shiki nord rotated shadow with-title" style="background-color: #1E1E1E; color: #D4D4D4; --agent: smith;" onclick="return doTheThing(&quot;now&quot;) &amp;&amp; succeed()">`)
})