Skip to content

armgit / bento

Public Imported from GitHub
0

Bento, the office suite that fits in a file

Make it possible to theme code snippets (#450)

Rahul Ravikumaraccf858ea36a
diff --git a/slides/src/code.ts b/slides/src/code.ts
index 783adf31..f62d45d6 100644
--- a/slides/src/code.ts
+++ b/slides/src/code.ts
@@ -18,11 +18,11 @@ import { BentoDoc, CodeElement } from './model'
import { HeckelDiff, type Match, type Insert, type Delete } from './diff'
/**
- * Seven colours, not four hundred scopes — the zero-cost tier. When the
+ * 8 Colors, not four hundred scopes — the zero-cost tier. When the
* signed-extension tier lands, grammarAssetId/themeAssetId select real
* TextMate rendering and this map becomes the fallback.
*/
-const CODE_COLORS: Record<string, string> = {
+const DEFAULT_CODE_COLORS: Record<string, string> = {
c: '#6b7f8f', // comment
s: '#c98a3e', // string
n: '#b0688f', // number
@@ -72,6 +72,7 @@ function diffFor(doc: BentoDoc, morphKey: string) {
*/
export function renderCodeInto(pre: HTMLElement, el: CodeElement, doc: BentoDoc): boolean {
const morphKey = el.morphId ?? el.id
+ const codePalette = doc.theme?.codePalette as any
// REFERENCE equality, not id: the duplicate-a-slide idiom gives every twin
// the SAME element id across slides (that is the default morph pairing), so
// an id lookup finds the first twin and every later slide would render the
@@ -100,7 +101,9 @@ export function renderCodeInto(pre: HTMLElement, el: CodeElement, doc: BentoDoc)
// diff.ts fromTok has already split multiline tokens, so ink never
// carries a newline here.
if (/\S/.test(token.content)) span.dataset.tok = ''
- const color = CODE_COLORS[token.scopes[0] ?? 'x']
+ // Derive the color from the codePalette if it exists.
+ // Otherwise, gracefully fallback to the default colors.
+ const color = codePalette?.[token.scopes[0]] ?? DEFAULT_CODE_COLORS[token.scopes[0] ?? 'x']
if (color) span.style.color = color
const kind = token.scopes[0]
if (kind === 'k' || kind === 'f') span.style.fontWeight = '600'
diff --git a/slides/src/model.ts b/slides/src/model.ts
index 8a89cd1d..ceda1373 100644
--- a/slides/src/model.ts
+++ b/slides/src/model.ts
@@ -456,6 +456,25 @@ export interface BentoDoc {
chartPalette?: string[]
/** defaults for newly inserted tables; omitted decks keep the standard look */
table?: Partial<TableStyle>
+ /** Code Palette (Tier-0). */
+ codePalette?: {
+ // comment
+ c?: string,
+ // string
+ s?: string,
+ // number
+ n?: string,
+ // keyword
+ k?: string,
+ // function calls
+ f?: string,
+ // punctutations
+ p?: string,
+ // diff: additions
+ a?: string,
+ // diff: deletions / removals
+ d?: string,
+ }
}
/** present-mode chrome; decks with built-in chrome can turn Reveal's off */
present?: {
diff --git a/slides/src/starterdeck.ts b/slides/src/starterdeck.ts
index daab6461..ef2c0b34 100644
--- a/slides/src/starterdeck.ts
+++ b/slides/src/starterdeck.ts
@@ -321,6 +321,22 @@ export function starterDoc(): BentoDoc {
doc.theme.accent = PEACH
// new charts (+ Chart, table→chart) inherit the deck's midnight-&-peach family
doc.theme.chartPalette = [PEACH, STEEL, PEACH_SOFT, STEEL_SOFT, MIST, PEACH_DEEP]
+ /**
+ * Code Palette:
+ * 8 colours, not four hundred scopes — the zero-cost tier. When the
+ * signed-extension tier lands, grammarAssetId/themeAssetId select real
+ * TextMate rendering and this map becomes the fallback.
+ */
+ doc.theme.codePalette = {
+ c: '#6b7f8f', // comment
+ s: '#c98a3e', // string
+ n: '#b0688f', // number
+ k: '#5b8def', // keyword
+ f: '#3fa9a0', // call
+ p: '#7c8794', // punctuation
+ a: '#3f9142', // diff: added
+ d: '#c25a43', // diff: removed
+ }
doc.fonts = [
{ family: 'Fraunces', asset: 'font-fraunces-900', weight: '900' },
{ family: 'Instrument Sans', asset: 'font-instrument', weight: '400 700' },