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 customizing Shiki. Fix some highlighting bugs. #22

Merged
merged 1 commit into from
Aug 28, 2024
Merged
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
5 changes: 5 additions & 0 deletions docs/src/basics/configuration.dj
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,9 @@ html:
# If you use TypeDoc, you'd want to set this to
# path/to/typedoc/**/*.css
ignore_css: ['some/css/you/dont/want.css']

features:
syntax_highlighting:
theme_light: vitesse-light
theme_dark: vitesse-dark
```
2 changes: 1 addition & 1 deletion docs/src/basics/custom_markup.dj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ The details of this markup are likely to change, since Djockey is experimental.

## Overriding HTML tags

Djot does not ([yet](https://github.com/jgm/djot/issues/240)) support arbitrary HTML tags in its input or output. Djockey works around this by postprocessing Djot's HTML output. Whenever you add a `tag=foo`{.language-plaintext} attribute, Djockey will replace the element's tag with the attribute's value.
Djot does not ([yet](https://github.com/jgm/djot/issues/240)) support arbitrary HTML tags in its input or output. Djockey works around this by postprocessing Djot's HTML output. Whenever you add a `tag=foo`{.language-text} attribute, Djockey will replace the element's tag with the attribute's value.

{% can't use .dj-djot-demo for features that require the entire Djockey pipeline (yet) %}

Expand Down
2 changes: 1 addition & 1 deletion docs/src/basics/frontmatter.dj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ order: 1

You can customize the title of your doc by adding YAML between `---` lines at the top, like this:

```plaintext
```
---
title: Custom Title
---
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/djotDemoPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class DjotDemoPlugin implements DjockeyPlugin {
tag: "code_block",
lang: "djot",
text: node.text,
attributes: { class: "language-plaintext" },
attributes: { class: "language-text" },
pos: structuredClone(node.pos),
},
{
Expand Down
28 changes: 18 additions & 10 deletions src/plugins/syntaxHighlighting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,34 @@ let nextID = 0;
export class SyntaxHighlightingPlugin implements DjockeyPlugin {
name = "Syntax Highlighting";

languages = new Set(["djot"].concat(Object.keys(bundledLanguages)));
languages = new Set(["djot", "text"].concat(Object.keys(bundledLanguages)));

highlightRequests: Record<string, { text: string; lang: string }> = {};
highlightResults: Record<string, string> = {};

djotHighlighter!: HighlighterGeneric<BundledLanguage, BundledTheme>;

constructor(public config: DjockeyConfigResolved) {}
themeLight: string;
themeDark: string;

constructor(public config: DjockeyConfigResolved) {
this.themeLight =
config.features?.syntax_highlighting?.theme_light ?? "vitesse-light";
this.themeDark =
config.features?.syntax_highlighting?.theme_dark ?? "vitesse-dark";
}

async setup() {
this.djotHighlighter = await createHighlighter({
langs: [djotTextmateGrammar as unknown as LanguageRegistration],
themes: ["vitesse-light", "vitesse-dark"],
themes: [this.themeLight, this.themeDark],
});
}

async highlight(text: string, lang: string): Promise<string> {
const themes = {
light: "vitesse-light",
dark: "vitesse-dark",
light: this.themeLight,
dark: this.themeDark,
};

try {
Expand Down Expand Up @@ -69,19 +77,19 @@ export class SyntaxHighlightingPlugin implements DjockeyPlugin {
code_block: (node: CodeBlock) => {
if (node.attributes?.hlRequestID) return; // Already scheduled

if (!this.languages.has(node.lang ?? "<none>")) {
return;
}

if (node.lang === "mermaid") {
// Special case: Mermaid renders as a diagram
return;
}

const lang = this.languages.has(node.lang ?? "<none>")
? node.lang ?? "text"
: "text";

const hlRequestID = `${nextID++}`;
this.highlightRequests[hlRequestID] = {
text: node.text,
lang: node.lang ?? "text",
lang,
};
const result: CodeBlock = {
...node,
Expand Down
7 changes: 7 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ export type DjockeyConfig = {
footer_text: string;
ignore_css?: string[];
};

features?: {
syntax_highlighting?: {
theme_light: string;
theme_dark: string;
};
};
};

export type DjockeyConfigResolved = DjockeyConfig & {
Expand Down
Loading