Skip to content

Commit

Permalink
docs: Add highlighting for devicetree and kconfig
Browse files Browse the repository at this point in the history
Added syntax highlighting for devicetree and kconfig files.

The PrismJS project is not accepting contributions right now as they
work on a version 2 of the library, so the new language files are added
directly here.

Also enabled syntax highlighting for various languages that are used in
the docs but aren't enabled in Docusaurus by default.
  • Loading branch information
joelspadin committed Oct 7, 2023
1 parent fd05478 commit 79a0a26
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 0 deletions.
13 changes: 13 additions & 0 deletions docs/docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ module.exports = {
colorMode: {
respectPrefersColorScheme: true,
},
prism: {
additionalLanguages: [
"bash",
"c",
"cmake",
"ini",
"linker-script",
"log",
"powershell",
],
theme: require("./src/theme/prism/themes/oceanic-next"),
darkTheme: require("./src/theme/prism/themes/oceanic-next"),
},
// sidebarCollapsible: false,
navbar: {
title: "ZMK Firmware",
Expand Down
23 changes: 23 additions & 0 deletions docs/src/theme/prism-include-languages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import siteConfig from "@generated/docusaurus.config";
export default function prismIncludeLanguages(PrismObject) {
const {
themeConfig: { prism },
} = siteConfig;
const { additionalLanguages } = prism;
// Prism components work on the Prism instance on the window, while prism-
// react-renderer uses its own Prism instance. We temporarily mount the
// instance onto window, import components to enhance it, then remove it to
// avoid polluting global namespace.
// You can mutate PrismObject: registering plugins, deleting languages... As
// long as you don't re-assign it
globalThis.Prism = PrismObject;
additionalLanguages.forEach((lang) => {
// eslint-disable-next-line global-require
require(`prismjs/components/prism-${lang}`);
});

require("./prism/components/prism-devicetree.js");
require("./prism/components/prism-kconfig.js");

delete globalThis.Prism;
}
45 changes: 45 additions & 0 deletions docs/src/theme/prism/components/prism-devicetree.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* Copyright (c) 2023 The ZMK Contributors
*
* SPDX-License-Identifier: MIT
*/

/* eslint-disable no-undef */

Prism.languages.devicetree = {
comment: Prism.languages.c["comment"],
string: Prism.languages.c["string"],
keyword:
/\/(?:bits|delete-node|delete-property|dts-v1|incbin|include|memreserve|omit-if-no-ref|plugin)\//,
label: {
pattern: /\b(?:[a-z_]\w*):/i,
alias: "tag",
},
reference: {
pattern: /&(?:[a-z_]\w*|\{[\w,.+*#?@/-]*\})/i,
alias: "variable",
},
node: {
pattern: /(?:\/|\b[\w,.+\-@]+)(?=\s*\{)/,
alias: "function",
inside: {
// Node address
number: {
pattern: /(@)[0-9a-f,]/i,
lookbehind: true,
},
},
},
function: Prism.languages.c["function"],
"attr-name": /\\?[\w,.+*#?@-]+(?=\s*[=;])/,
number: [/\b[0-9a-f]{2}\b/i, /\b(?:0[xX][0-9a-fA-F]+|\d+)(?:ULL|UL|LL|U|L)?/],
constant: {
pattern: /\b[A-Z_]+\b/,
alias: "number",
},
macro: Prism.languages.c["macro"],
operator: /<<|>>|[<>]=?|[!=]=?|&&?|\|\|?|[+\-*/%~?^]/,
punctuation: /[{}[\];(),.]/,
};

Prism.languages.dts = Prism.languages.devicetree;
44 changes: 44 additions & 0 deletions docs/src/theme/prism/components/prism-kconfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Copyright (c) 2023 The ZMK Contributors
*
* SPDX-License-Identifier: MIT
*/

/* eslint-disable no-undef */

Prism.languages.kconfig = {
comment: {
pattern: /(^|[^\\])#.*/,
lookbehind: true,
greedy: true,
},
string: /"(?:\\.|[^\\\r\n"])*"/,
helptext: {
// help text ends at the first line at a lower level of indentation than the
// first line of text.
pattern: /(^\s*)(?:help|---help---)\s*^(\s+)(?:.+)(?:\s*^\2[^\n]*)*/m,
lookbehind: true,
alias: "string",
inside: {
keyword: /^(?:help|---help---)/,
},
},
keyword:
/\b(?:allnoconfig_y|bool|boolean|choice|comment|config|def_bool|def_hex|def_int|def_string|def_tristate|default|defconfig_list|depends|endchoice|endif|endmenu|env|hex|if|imply|int|mainmenu|menu|menuconfig|modules|on|option|optional|orsource|osource|prompt|range|rsource|select|source|string|tristate|visible)\b/,
expansion: {
pattern: /\$\([\s\S]+\)/,
alias: "variable",
inside: {
function: /\$\(|\)/,
punctuation: /,/,
},
},
number: /\b(?:0[xX][0-9a-fA-F]+|\d+)/,
boolean: {
pattern: /\b(?:y|n|m)\b/,
alias: "number",
},
variable: /\b[A-Z_]+\b/,
operator: /[<>]=?|[!=]=?|&&|\|\|/,
punctuation: /[()]/,
};
31 changes: 31 additions & 0 deletions docs/src/theme/prism/themes/oceanic-next.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/** @type {import("prism-react-renderer").PrismTheme} */
const baseTheme = require("prism-react-renderer/themes/oceanicNext");

/** @type {import("prism-react-renderer").PrismTheme} */
const theme = {
plain: baseTheme.plain,
styles: [
...baseTheme.styles,
{
types: ["directive", "directive-hash"],
style: {
color: "#c5a5c5",
},
},
// Default colors for diffs are unintuitive. Switch to red and green.
{
types: ["deleted"],
style: {
color: "#fc929e",
},
},
{
types: ["inserted"],
style: {
color: "#8dc891",
},
},
],
};

module.exports = theme;

0 comments on commit 79a0a26

Please sign in to comment.