From 818a76bd550c649abb80bda0c5b23e1f47d251c6 Mon Sep 17 00:00:00 2001 From: kazk Date: Wed, 22 Jun 2022 16:59:56 -0700 Subject: [PATCH 1/2] Update shiki and add Factor syntax --- package.json | 2 +- src/remark/shiki/index.js | 152 +- src/remark/shiki/languages/README.md | 9 +- .../shiki/languages/factor.tmLanguage.json | 204 +++ .../shiki/languages/nim.tmLanguage.json | 1521 ----------------- src/remark/shiki/languages/r.tmLanguage.json | 540 ------ .../shiki/languages/raku.tmLanguage.json | 315 ---- yarn.lock | 8 +- 8 files changed, 251 insertions(+), 2500 deletions(-) create mode 100644 src/remark/shiki/languages/factor.tmLanguage.json delete mode 100644 src/remark/shiki/languages/nim.tmLanguage.json delete mode 100644 src/remark/shiki/languages/r.tmLanguage.json delete mode 100644 src/remark/shiki/languages/raku.tmLanguage.json diff --git a/package.json b/package.json index a14cdd98..c7c9c4b9 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "postcss-nested": "^5.0.5", "postcss-preset-env": "^6.7.0", "prettier": "^2.2.1", - "shiki": "^0.9.3", + "shiki": "^0.10.1", "tailwindcss": "^2.2.2", "unist-util-visit": "^2.0.3" } diff --git a/src/remark/shiki/index.js b/src/remark/shiki/index.js index bd8b62d6..b549f136 100644 --- a/src/remark/shiki/index.js +++ b/src/remark/shiki/index.js @@ -1,75 +1,15 @@ +const fs = require("fs"); const path = require("path"); -const { getHighlighter, BUNDLED_LANGUAGES } = require("shiki"); +const { getHighlighter } = require("shiki"); const visit = require("unist-util-visit"); -const syntaxPath = (name) => - path.resolve(__dirname, `./languages/${name}.tmLanguage.json`); - -const langs = new Set([ - "asm", - "c", - "clojure", - "cobol", - "coffee", - "cpp", - "crystal", - "csharp", - "css", - "d", - "dart", - "elixir", - "elm", - "erlang", - "fsharp", - "go", - "groovy", - "haskell", - "html", - "java", - "javascript", - "json", - "jsonc", - "jsx", - "julia", - "kotlin", - "latex", - "lisp", - "lua", - "markdown", - "mdx", - "nim", - "objc", - "ocaml", - "pascal", - "perl", - "php", - "powershell", - "prolog", - "purescript", - "python", - "r", - "raku", - "ruby", - "rust", - "scala", - "shell", - "solidity", - "sql", - "swift", - "toml", - "tsx", - "typescript", - "vb", - "yaml", -]); - -const languages = BUNDLED_LANGUAGES.filter((lang) => { - return ( - langs.has(lang.id) || - (lang.aliases && lang.aliases.some((id) => langs.has(id))) +const grammar = (name) => + JSON.parse( + fs.readFileSync( + path.resolve(__dirname, `./languages/${name}.tmLanguage.json`) + ) ); -}); // Remap language id to match Shiki. const remapLanguageId = (id) => { @@ -88,50 +28,40 @@ const remapLanguageId = (id) => { // or https://github.com/andrewbranch/gatsby-remark-vscode#line-highlighting const createHighlighter = ({ theme = "nord" } = {}) => { // Reuse the same instance - const highlighterPromise = getHighlighter({ - theme, - langs: languages.concat([ - // See languages/README.md for sources. - // TODO Open PR upstream - { - id: "fortran", - scopeName: "source.fortran.free", - path: syntaxPath("fortran"), - }, - { - id: "idris", - scopeName: "source.idris", - path: syntaxPath("idris"), - }, - { - id: "haxe", - scopeName: "source.hx", - path: syntaxPath("haxe"), - }, - { - id: "racket", - scopeName: "source.racket", - path: syntaxPath("racket"), - }, - - // TODO Remove these after a new shiki is released - { - id: "nim", - scopeName: "source.nim", - path: syntaxPath("nim"), - }, - { - id: "r", - scopeName: "source.r", - path: syntaxPath("r"), - }, - { - id: "raku", - scopeName: "source.perl.6", - path: syntaxPath("raku"), - }, - ]), - }); + const highlighterPromise = getHighlighter({ theme }).then( + async (highlighter) => { + const langs = [ + // See languages/README.md for sources. + { + id: "factor", + scopeName: "source.factor", + grammar: grammar("factor"), + }, + { + id: "fortran", + scopeName: "source.fortran.free", + grammar: grammar("fortran"), + }, + { + id: "idris", + scopeName: "source.idris", + grammar: grammar("idris"), + }, + { + id: "haxe", + scopeName: "source.hx", + grammar: grammar("haxe"), + }, + { + id: "racket", + scopeName: "source.racket", + grammar: grammar("racket"), + }, + ]; + for (const lang of langs) await highlighter.loadLanguage(lang); + return highlighter; + } + ); return () => async (tree) => { const highlighter = await highlighterPromise; diff --git a/src/remark/shiki/languages/README.md b/src/remark/shiki/languages/README.md index 839e9a6b..69f0b2b9 100644 --- a/src/remark/shiki/languages/README.md +++ b/src/remark/shiki/languages/README.md @@ -1,14 +1,7 @@ Additional languages for shiki. +- Factor https://github.com/DexterHaslem/vscode-factor - Fortran https://github.com/krvajal/vscode-fortran-support - Haxe https://github.com/vshaxe/haxe-TmLanguage - Idris https://github.com/zjhmale/vscode-idris - Racket https://github.com/pouyakary/vscode-racket - -## Included in unreleased version - -These should be removed when a new version of shiki is released. - -- Nim https://github.com/pragmagic/vscode-nim -- R https://github.com/Ikuyadeu/vscode-R -- Raku https://github.com/Raku/atom-language-perl6 diff --git a/src/remark/shiki/languages/factor.tmLanguage.json b/src/remark/shiki/languages/factor.tmLanguage.json new file mode 100644 index 00000000..9d92692a --- /dev/null +++ b/src/remark/shiki/languages/factor.tmLanguage.json @@ -0,0 +1,204 @@ +{ + "fileTypes": [ + "factor", + "facts" + ], + "foldingStartMarker": "^\\s*(M|C)?:|#(V|H|C)\\{", + "foldingStopMarker": "^\\s*$", + "keyEquivalent": "^~F", + "name": "Factor", + "patterns": [ + { + "captures": { + "2": { + "name": "keyword.colon.factor" + }, + "3": { + "name": "entity.name.function.factor" + }, + "4": { + "name": "comment.stack-effect.factor" + } + }, + "match": "(^|(?<=\\s))(:)\\s+([^\\s]+)\\s", + "name": "definition.word.factor" + }, + { + "captures": { + "2": { + "name": "keyword.colon.factor" + }, + "3": { + "name": "entity.name.method.factor" + }, + "4": { + "name": "comment.stack-effect.factor" + } + }, + "match": "(^|(?<=\\s))(C:)\\s+([^\\s]+)\\s", + "name": "definition.word.class.factor" + }, + { + "captures": { + "2": { + "name": "keyword.colon.factor" + }, + "3": { + "name": "entity.name.class.factor" + }, + "4": { + "name": "entity.name.method.factor" + }, + "5": { + "name": "comment.stack-effect.factor" + } + }, + "match": "(^|(?<=\\s))(M:)\\s+([^\\s]+)\\s+([^\\s]+)\\s", + "name": "definition.word.method.factor" + }, + { + "captures": { + "2": { + "name": "keyword.colon.factor" + }, + "4": { + "name": "entity.name.generic.factor" + }, + "5": { + "name": "comment.stack-effect.factor" + } + }, + "match": "(^|(?<=\\s))(G(ENERIC)?:)\\s+([^\\s]+)\\s", + "name": "definition.word.generic.factor" + }, + { + "captures": { + "2": { + "name": "storage.type.factor" + }, + "3": { + "name": "entity.name.class.factor" + } + }, + "match": "(^|(?<=\\s))(TUPLE:)\\s+([^\\s]+)\\s+(([^\\s]+)+)", + "name": "meta.class.factor" + }, + { + "match": "(^|(?<=\\s))(drop|2drop|3drop|keep|2keep|3keep|nip|2nip|dup|2dup|3dup|dupd|over|pick|tuck|swap|rot|-rot|roll|-roll|slip|2swap|swapd|>r|r>)(\\s|$)", + "name": "keyword.control.stack.factor" + }, + { + "match": "(^|(?<=\\s))(get|set)(\\s|$)", + "name": "keyword.control.variable.factor" + }, + { + "match": "(^|(?<=\\s))(if|when|unless|if*|unless*|when*)\\*?(\\s|$)", + "name": "keyword.control.flow.factor" + }, + { + "begin": "(^|(?<=\\s))(USING:)", + "end": "(?<=\\s);(\\s|$)", + "name": "keyword.control.using.factor", + "patterns": [ + { + "match": "(^|(?<=\\s))[^\\s]+(\\s|$)", + "name": "constant.namespace.factor" + } + ] + }, + { + "begin": "(^|(?<=\\s))(REQUIRES:)", + "end": "(?<=\\s);(\\s|$)", + "name": "keyword.control.requires.factor", + "patterns": [ + { + "match": "(^|(?<=\\s))[^\\s]+(\\s|$)", + "name": "constant.namespace.factor" + } + ] + }, + { + "match": "(^|(?<=\\s))(f|t)(\\s|$)", + "name": "constant.language.factor" + }, + { + "match": "(^|(?<=\\s))CHAR:\\s+[^\\s]+(\\s|$)", + "name": "constant.character.factor" + }, + { + "match": "(^|(?<=\\s))-?+\\d+\\.\\d+(E(\\+|-)\\d+)?(\\s|$)", + "name": "constant.numeric.float.factor" + }, + { + "match": "(^|(?<=\\s))-?\\d+/-?\\d+(\\s|$)", + "name": "constant.numeric.rational.factor" + }, + { + "begin": "\"", + "end": "\"", + "name": "string.quoted.double.factor", + "patterns": [ + { + "include": "#escaped_characters" + } + ] + }, + { + "begin": "<\"", + "end": "\">", + "name": "string.quoted.double.multiline.factor", + "patterns": [ + { + "include": "#escaped_characters" + } + ] + }, + { + "begin": "(^|(?<=\\s))(STRING:)\\s+(\\S+)", + "captures": { + "2": { + "name": "keyword.colon.factor" + }, + "3": { + "name": "entity.name.heredoc.factor" + } + }, + "contentName": "string.unquoted.heredoc.factor", + "end": "^;$", + "name": "definition.word.heredoc.factor" + }, + { + "match": "inline|foldable", + "name": "storage.modifier.factor" + }, + { + "match": "(^|(?<=\\s))#?!(\\s.*)?$", + "name": "comment.line.factor" + }, + { + "begin": "\\((?=\\s)", + "end": "(^|(?<=\\s))\\)", + "name": "comment.parens.factor" + }, + { + "match": "\\b[^\\s]+:\\s+[^\\s]+(\\s|$)", + "name": "keyword.control.postpone.factor" + } + ], + "repository": { + "escaped_characters": { + "patterns": [ + { + "match": "\\\\(\\\\|[enrts'\"?]|[0-3]\\d{,2}|[4-7]\\d?|u[a-fA-F0-9]{,2})", + "name": "constant.character.escape.factor" + }, + { + "match": "\\\\.", + "name": "invalid.illegal.unknown-escape.factor" + } + ] + } + }, + "scopeName": "source.factor", + "uuid": "3C9C9C2A-314A-475B-A4E4-A68BAAF3F36E" +} diff --git a/src/remark/shiki/languages/nim.tmLanguage.json b/src/remark/shiki/languages/nim.tmLanguage.json deleted file mode 100644 index 02d291db..00000000 --- a/src/remark/shiki/languages/nim.tmLanguage.json +++ /dev/null @@ -1,1521 +0,0 @@ -{ - "fileTypes": ["nim"], - "keyEquivalent": "^~N", - "name": "nim", - "patterns": [ - { - "begin": "[ \\t]*##\\[", - "contentName": "comment.block.doc-comment.content.nim", - "end": "\\]##", - "name": "comment.block.doc-comment.nim", - "patterns": [ - { - "include": "#multilinedoccomment", - "name": "comment.block.doc-comment.nested.nim" - } - ] - }, - { - "begin": "[ \\t]*#\\[", - "contentName": "comment.block.content.nim", - "end": "\\]#", - "name": "comment.block.nim", - "patterns": [ - { - "include": "#multilinecomment", - "name": "comment.block.nested.nim" - } - ] - }, - { - "begin": "(^[ \\t]+)?(?=##)", - "beginCaptures": { - "1": { - "name": "punctuation.whitespace.comment.leading.nim" - } - }, - "end": "(?!\\G)", - "patterns": [ - { - "begin": "##", - "beginCaptures": { - "0": { - "name": "punctuation.definition.comment.nim" - } - }, - "end": "\\n", - "name": "comment.line.number-sign.doc-comment.nim" - } - ] - }, - { - "begin": "(^[ \\t]+)?(?=#[^\\[])", - "beginCaptures": { - "1": { - "name": "punctuation.whitespace.comment.leading.nim" - } - }, - "end": "(?!\\G)", - "patterns": [ - { - "begin": "#", - "beginCaptures": { - "0": { - "name": "punctuation.definition.comment.nim" - } - }, - "end": "\\n", - "name": "comment.line.number-sign.nim" - } - ] - }, - { - "comment": "A nim procedure or method", - "name": "meta.proc.nim", - "patterns": [ - { - "begin": "\\b(proc|method|template|macro|iterator|converter|func)\\s+\\`?([^\\:\\{\\s\\`\\*\\(]*)\\`?(\\s*\\*)?\\s*(?=\\(|\\=|:|\\[|\\n|\\{)", - "captures": { - "1": { - "name": "keyword.other" - }, - "2": { - "name": "entity.name.function.nim" - }, - "3": { - "name": "keyword.control.export" - } - }, - "end": "\\)", - "patterns": [ - { - "include": "source.nim" - } - ] - } - ] - }, - { - "begin": "discard \"\"\"", - "comment": "A discarded triple string literal comment", - "end": "\"\"\"(?!\")", - "name": "comment.line.discarded.nim" - }, - { - "include": "#float_literal" - }, - { - "include": "#integer_literal" - }, - { - "comment": "Operator as function name", - "match": "(?<=\\`)[^\\` ]+(?=\\`)", - "name": "entity.name.function.nim" - }, - { - "captures": { - "1": { - "name": "keyword.control.export" - } - }, - "comment": "Export qualifier.", - "match": "\\b\\s*(\\*)(?:\\s*(?=[,:])|\\s+(?=[=]))" - }, - { - "comment": "Export qualifier following a type def.", - "match": "\\b([A-Z]\\w+)(\\*)", - "captures": { - "1": { - "name": "support.type.nim" - }, - "2": { - "name": "keyword.control.export" - } - } - }, - { - "include": "#string_literal" - }, - { - "comment": "Language Constants.", - "match": "\\b(true|false|Inf|NegInf|NaN|nil)\\b", - "name": "constant.language.nim" - }, - { - "comment": "Keywords that affect program control flow or scope.", - "match": "\\b(block|break|case|continue|do|elif|else|end|except|finally|for|if|raise|return|try|when|while|yield)\\b", - "name": "keyword.control.nim" - }, - { - "comment": "Keyword boolean operators for expressions.", - "match": "(\\b(and|in|is|isnot|not|notin|or|xor)\\b)", - "name": "keyword.boolean.nim" - }, - { - "comment": "Generic operators for expressions.", - "match": "(=|\\+|-|\\*|/|<|>|@|\\$|~|&|%|!|\\?|\\^|\\.|:|\\\\)+", - "name": "keyword.operator.nim" - }, - { - "comment": "Other keywords.", - "match": "(\\b(addr|as|asm|atomic|bind|cast|const|converter|concept|defer|discard|distinct|div|enum|export|from|import|include|let|mod|mixin|object|of|ptr|ref|shl|shr|static|type|using|var|tuple|iterator|macro|func|method|proc|template)\\b)", - "name": "keyword.other.nim" - }, - { - "comment": "Invalid and unused keywords.", - "match": "(\\b(generic|interface|lambda|out|shared)\\b)", - "name": "invalid.illegal.invalid-keyword.nim" - }, - { - "comment": "Common functions", - "match": "\\b(new|await|assert|echo|defined|declared|newException|countup|countdown|high|low)\\b", - "name": "keyword.other.common.function.nim" - }, - { - "comment": "Built-in, concrete types.", - "match": "\\b(((uint|int)(8|16|32|64)?)|float(32|64)?|bool|string|auto|cstring|char|byte|tobject|typedesc|stmt|expr|any|untyped|typed)\\b", - "name": "storage.type.concrete.nim" - }, - { - "comment": "Built-in, generic types.", - "match": "\\b(range|array|seq|set|pointer)\\b", - "name": "storage.type.generic.nim" - }, - { - "comment": "Special types.", - "match": "\\b(openarray|varargs|void)\\b", - "name": "storage.type.generic.nim" - }, - { - "comment": "Other constants.", - "match": "\\b[A-Z][A-Z0-9_]+\\b", - "name": "support.constant.nim" - }, - { - "comment": "Other types.", - "match": "\\b[A-Z]\\w+\\b", - "name": "support.type.nim" - }, - { - "comment": "Function call.", - "match": "\\b\\w+\\b(?=(\\[([a-zA-Z0-9_,]|\\s)+\\])?\\()", - "name": "support.function.any-method.nim" - }, - { - "comment": "Function call (no parenthesis).", - "match": "(?!(openarray|varargs|void|range|array|seq|set|pointer|new|await|assert|echo|defined|declared|newException|countup|countdown|high|low|((uint|int)(8|16|32|64)?)|float(32|64)?|bool|string|auto|cstring|char|byte|tobject|typedesc|stmt|expr|any|untyped|typed|addr|as|asm|atomic|bind|cast|const|converter|concept|defer|discard|distinct|div|enum|export|from|import|include|let|mod|mixin|object|of|ptr|ref|shl|shr|static|type|using|var|tuple|iterator|macro|func|method|proc|template|and|in|is|isnot|not|notin|or|xor|proc|method|template|macro|iterator|converter|func|true|false|Inf|NegInf|NaN|nil|block|break|case|continue|do|elif|else|end|except|finally|for|if|raise|return|try|when|while|yield)\\b)\\w+\\s+(?!(and|in|is|isnot|not|notin|or|xor|[^a-zA-Z0-9_\"'`(-+]+)\\b)(?=[a-zA-Z0-9_\"'`(-+])", - "name": "support.function.any-method.nim" - }, - { - "begin": "(^\\s*)?(?=\\{\\.emit: ?\"\"\")", - "beginCaptures": { - "0": { - "name": "punctuation.whitespace.embedded.leading.nim" - } - }, - "end": "(?!\\G)(\\s*$\\n?)?", - "endCaptures": { - "0": { - "name": "punctuation.whitespace.embedded.trailing.nim" - } - }, - "patterns": [ - { - "begin": "\\{\\.(emit:) ?(\"\"\")", - "captures": { - "1": { - "name": "keyword.other.nim" - }, - "2": { - "name": "punctuation.section.embedded.begin.nim" - } - }, - "contentName": "source.c", - "end": "(\")\"\"(?!\")(\\.{0,1}\\})?", - "endCaptures": { - "0": { - "name": "punctuation.section.embedded.end.nim" - }, - "1": { - "name": "source.c" - } - }, - "name": "meta.embedded.block.c", - "patterns": [ - { - "begin": "\\`", - "end": "\\`", - "name": "keyword.operator.nim" - }, - { - "include": "source.c" - } - ] - } - ] - }, - { - "begin": "\\{\\.", - "beginCaptures": { - "0": { - "name": "punctuation.pragma.start.nim" - } - }, - "end": "\\.?\\}", - "endCaptures": { - "0": { - "name": "punctuation.pragma.end.nim" - } - }, - "patterns": [ - { - "begin": "\\b([[:alpha:]]\\w*)(?:\\s|\\s*:)", - "beginCaptures": { - "1": { - "name": "meta.preprocessor.pragma.nim" - } - }, - "end": "(?=\\.?\\}|,)", - "patterns": [ - { - "include": "source.nim" - } - ] - }, - { - "begin": "\\b([[:alpha:]]\\w*)\\(", - "beginCaptures": { - "1": { - "name": "meta.preprocessor.pragma.nim" - } - }, - "end": "\\)", - "patterns": [ - { - "include": "source.nim" - } - ] - }, - { - "match": "\\b([[:alpha:]]\\w*)(?=\\.?\\}|,)", - "captures": { - "1": { - "name": "meta.preprocessor.pragma.nim" - } - } - }, - { - "begin": "\\b([[:alpha:]]\\w*)(\"\"\")", - "beginCaptures": { - "1": { - "name": "meta.preprocessor.pragma.nim" - }, - "2": { - "name": "punctuation.definition.string.begin.nim" - } - }, - "end": "\"\"\"(?!\")", - "endCaptures": { - "0": { - "name": "punctuation.definition.string.end.nim" - } - }, - "name": "string.quoted.triple.raw.nim" - }, - { - "begin": "\\b([[:alpha:]]\\w*)(\")", - "beginCaptures": { - "1": { - "name": "meta.preprocessor.pragma.nim" - }, - "2": { - "name": "punctuation.definition.string.begin.nim" - } - }, - "end": "\"", - "endCaptures": { - "0": { - "name": "punctuation.definition.string.end.nim" - } - }, - "name": "string.quoted.double.raw.nim" - }, - { - "begin": "\\b(hint\\[\\w+\\]):", - "beginCaptures": { - "1": { - "name": "meta.preprocessor.pragma.nim" - } - }, - "end": "(?=\\.?\\}|,)", - "patterns": [ - { - "include": "source.nim" - } - ] - }, - { - "match": ",", - "name": "punctuation.separator.comma.nim" - } - ] - }, - { - "begin": "(^\\s*)?(?=asm \"\"\")", - "beginCaptures": { - "0": { - "name": "punctuation.whitespace.embedded.leading.nim" - } - }, - "end": "(?!\\G)(\\s*$\\n?)?", - "endCaptures": { - "0": { - "name": "punctuation.whitespace.embedded.trailing.nim" - } - }, - "patterns": [ - { - "begin": "(asm) (\"\"\")", - "captures": { - "1": { - "name": "keyword.other.nim" - }, - "2": { - "name": "punctuation.section.embedded.begin.nim" - } - }, - "contentName": "source.asm", - "end": "(\")\"\"(?!\")", - "endCaptures": { - "0": { - "name": "punctuation.section.embedded.end.nim" - }, - "1": { - "name": "source.asm" - } - }, - "name": "meta.embedded.block.asm", - "patterns": [ - { - "begin": "\\`", - "end": "\\`", - "name": "keyword.operator.nim" - }, - { - "include": "source.asm" - } - ] - } - ] - }, - { - "captures": { - "1": { - "name": "storage.type.function.nim" - }, - "2": { - "name": "keyword.operator.nim" - } - }, - "comment": "tmpl specifier", - "match": "(tmpl(i)?)(?=( (html|xml|js|css|glsl|md))?\"\"\")" - }, - { - "begin": "(^\\s*)?(?=html\"\"\")", - "beginCaptures": { - "0": { - "name": "punctuation.whitespace.embedded.leading.nim" - } - }, - "end": "(?!\\G)(\\s*$\\n?)?", - "endCaptures": { - "0": { - "name": "punctuation.whitespace.embedded.trailing.nim" - } - }, - "patterns": [ - { - "begin": "(html)(\"\"\")", - "captures": { - "1": { - "name": "keyword.other.nim" - }, - "2": { - "name": "punctuation.section.embedded.begin.nim" - } - }, - "contentName": "text.html", - "end": "(\")\"\"(?!\")", - "endCaptures": { - "0": { - "name": "punctuation.section.embedded.end.nim" - }, - "1": { - "name": "text.html" - } - }, - "name": "meta.embedded.block.html", - "patterns": [ - { - "begin": "(?|->>)", - "name": "keyword.operator.assignment.r" - }, - { - "match": "(==|<=|>=|!=|<>|<|>|%in%)", - "name": "keyword.operator.comparison.r" - }, - { - "match": "(!|&{1,2}|[|]{1,2})", - "name": "keyword.operator.logical.r" - }, - { - "match": "(%between%|%chin%|%like%|%\\+%|%\\+replace%|%:%|%do%|%dopar%|%>%|%<>%|%T>%|%\\$%)", - "name": "keyword.operator.other.r" - }, - { - "match": "(\\.\\.\\.|\\$|:|\\~|@)", - "name": "keyword.other.r" - } - ] - }, - "storage-type": { - "patterns": [ - { - "match": "\\b(character|complex|double|expression|integer|list|logical|numeric|single|raw)\\b(?=\\s*\\()", - "name": "storage.type.r" - } - ] - }, - "strings": { - "patterns": [ - { - "begin": "\"", - "beginCaptures": { - "0": { - "name": "punctuation.definition.string.begin.r" - } - }, - "end": "\"", - "endCaptures": { - "0": { - "name": "punctuation.definition.string.end.r" - } - }, - "name": "string.quoted.double.r", - "patterns": [ - { - "match": "\\\\.", - "name": "constant.character.escape.r" - } - ] - }, - { - "begin": "'", - "beginCaptures": { - "0": { - "name": "punctuation.definition.string.begin.r" - } - }, - "end": "'", - "endCaptures": { - "0": { - "name": "punctuation.definition.string.end.r" - } - }, - "name": "string.quoted.single.r", - "patterns": [ - { - "match": "\\\\.", - "name": "constant.character.escape.r" - } - ] - } - ] - }, - "brackets": { - "patterns": [ - { - "begin": "\\(", - "beginCaptures": { - "0": { - "name": "punctuation.section.parens.begin.r" - } - }, - "end": "\\)", - "endCaptures": { - "0": { - "name": "punctuation.section.parens.end.r" - } - }, - "patterns": [ - { - "include": "source.r" - } - ] - }, - { - "begin": "\\[(?!\\[)", - "beginCaptures": { - "0": { - "name": "punctuation.section.brackets.single.begin.r" - } - }, - "end": "\\]", - "endCaptures": { - "0": { - "name": "punctuation.section.brackets.single.end.r" - } - }, - "patterns": [ - { - "include": "source.r" - } - ] - }, - { - "begin": "\\[\\[", - "beginCaptures": { - "0": { - "name": "punctuation.section.brackets.double.begin.r" - } - }, - "end": "\\]\\]", - "endCaptures": { - "0": { - "name": "punctuation.section.brackets.double.end.r" - } - }, - "contentName": "meta.item-access.arguments.r", - "patterns": [ - { - "include": "source.r" - } - ] - }, - { - "begin": "\\{", - "beginCaptures": { - "0": { - "name": "punctuation.section.braces.begin.r" - } - }, - "end": "\\}", - "endCaptures": { - "0": { - "name": "punctuation.section.braces.end.r" - } - }, - "patterns": [ - { - "include": "source.r" - } - ] - } - ] - }, - "function-declarations": { - "patterns": [ - { - "begin": "^\\s*([a-zA-Z._][\\w.:]*)\\s*(<)))?([a-zA-Z_\\x{C0}-\\x{FF}\\$])([a-zA-Z0-9_\\x{C0}-\\x{FF}\\$]|[\\-'][a-zA-Z0-9_\\x{C0}-\\x{FF}\\$])*", - "name": "variable.other.identifier.perl.6" - }, - { - "match": "\\b(eager|hyper|substr|index|rindex|grep|map|sort|join|lines|hints|chmod|split|reduce|min|max|reverse|truncate|zip|cat|roundrobin|classify|first|sum|keys|values|pairs|defined|delete|exists|elems|end|kv|any|all|one|wrap|shape|key|value|name|pop|push|shift|splice|unshift|floor|ceiling|abs|exp|log|log10|rand|sign|sqrt|sin|cos|tan|round|strand|roots|cis|unpolar|polar|atan2|pick|chop|p5chop|chomp|p5chomp|lc|lcfirst|uc|ucfirst|capitalize|normalize|pack|unpack|quotemeta|comb|samecase|sameaccent|chars|nfd|nfc|nfkd|nfkc|printf|sprintf|caller|evalfile|run|runinstead|nothing|want|bless|chr|ord|gmtime|time|eof|localtime|gethost|getpw|chroot|getlogin|getpeername|kill|fork|wait|perl|graphs|codes|bytes|clone|print|open|read|write|readline|say|seek|close|opendir|readdir|slurp|spurt|shell|run|pos|fmt|vec|link|unlink|symlink|uniq|pair|asin|atan|sec|cosec|cotan|asec|acosec|acotan|sinh|cosh|tanh|asinh|done|acos|acosh|atanh|sech|cosech|cotanh|sech|acosech|acotanh|asech|ok|nok|plan_ok|dies_ok|lives_ok|skip|todo|pass|flunk|force_todo|use_ok|isa_ok|diag|is_deeply|isnt|like|skip_rest|unlike|cmp_ok|eval_dies_ok|nok_error|eval_lives_ok|approx|is_approx|throws_ok|version_lt|plan|EVAL|succ|pred|times|nonce|once|signature|new|connect|operator|undef|undefine|sleep|from|to|infix|postfix|prefix|circumfix|postcircumfix|minmax|lazy|count|unwrap|getc|pi|e|context|void|quasi|body|each|contains|rewinddir|subst|can|isa|flush|arity|assuming|rewind|callwith|callsame|nextwith|nextsame|attr|eval_elsewhere|none|srand|trim|trim_start|trim_end|lastcall|WHAT|WHERE|HOW|WHICH|VAR|WHO|WHENCE|ACCEPTS|REJECTS|not|true|iterator|by|re|im|invert|flip|gist|flat|tree|is-prime|throws_like|trans)\\b", - "name": "support.function.perl" - } - ], - "repository": { - "qq_brace_string_content": { - "begin": "{", - "end": "}", - "patterns": [ - { - "include": "#qq_brace_string_content" - } - ] - }, - "qq_bracket_string_content": { - "begin": "\\[", - "end": "\\]", - "patterns": [ - { - "include": "#qq_bracket_string_content" - } - ] - }, - "qq_double_string_content": { - "begin": "\"", - "end": "\"", - "patterns": [ - { - "include": "#qq_double_string_content" - } - ] - }, - "qq_paren_string_content": { - "begin": "\\(", - "end": "\\)", - "patterns": [ - { - "include": "#qq_paren_string_content" - } - ] - }, - "qq_single_string_content": { - "begin": "'", - "end": "'", - "patterns": [ - { - "include": "#qq_single_string_content" - } - ] - }, - "qq_slash_string_content": { - "begin": "\\\\/", - "end": "\\\\/", - "patterns": [ - { - "include": "#qq_slash_string_content" - } - ] - } - } -} diff --git a/yarn.lock b/yarn.lock index fb209af8..d8bbd651 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7162,10 +7162,10 @@ shelljs@^0.8.5: interpret "^1.0.0" rechoir "^0.6.2" -shiki@^0.9.3: - version "0.9.15" - resolved "https://registry.yarnpkg.com/shiki/-/shiki-0.9.15.tgz#2481b46155364f236651319d2c18e329ead6fa44" - integrity sha512-/Y0z9IzhJ8nD9nbceORCqu6NgT9X6I8Fk8c3SICHI5NbZRLdZYFaB233gwct9sU0vvSypyaL/qaKvzyQGJBZSw== +shiki@^0.10.1: + version "0.10.1" + resolved "https://registry.yarnpkg.com/shiki/-/shiki-0.10.1.tgz#6f9a16205a823b56c072d0f1a0bcd0f2646bef14" + integrity sha512-VsY7QJVzU51j5o1+DguUd+6vmCmZ5v/6gYu4vyYAhzjuNQU6P/vmSy4uQaOhvje031qQMiW0d2BwgMH52vqMng== dependencies: jsonc-parser "^3.0.0" vscode-oniguruma "^1.6.1" From 6f839572ef51678a285ca6dc136651188b89533e Mon Sep 17 00:00:00 2001 From: kazk Date: Thu, 23 Jun 2022 16:10:26 -0700 Subject: [PATCH 2/2] Change code block theme and support light mode --- docusaurus.config.js | 6 +- src/css/custom.css | 14 +++++ src/remark/shiki/index.js | 112 ++++++++++++++++++++------------------ 3 files changed, 78 insertions(+), 54 deletions(-) diff --git a/docusaurus.config.js b/docusaurus.config.js index 9fb22a5d..caaa5cab 100644 --- a/docusaurus.config.js +++ b/docusaurus.config.js @@ -66,7 +66,11 @@ module.exports = { routeBasePath: "/", sidebarPath: require.resolve("./sidebars.js"), editUrl: "https://github.com/codewars/docs/edit/master/", - beforeDefaultRemarkPlugins: [createHighlighter({ theme: "nord" })], + beforeDefaultRemarkPlugins: [ + createHighlighter({ + themes: ["github-dark-dimmed", "github-light"], + }), + ], }, blog: false, theme: { diff --git a/src/css/custom.css b/src/css/custom.css index 362ea610..735e154d 100644 --- a/src/css/custom.css +++ b/src/css/custom.css @@ -181,3 +181,17 @@ html[data-theme="dark"] .DocSearch { var(--ifm-color-emphasis-100) 100% ); } + +html[data-theme="dark"] pre.github-light { + display: none; +} + +html[data-theme="light"] pre.github-dark-dimmed { + display: none; +} + +html[data-theme="light"] pre.github-light { + outline-style: solid; + outline-width: 1px; + outline-color: #d1d5db; +} diff --git a/src/remark/shiki/index.js b/src/remark/shiki/index.js index b549f136..2fa83db8 100644 --- a/src/remark/shiki/index.js +++ b/src/remark/shiki/index.js @@ -26,9 +26,9 @@ const remapLanguageId = (id) => { // Use shiki for syntax highlighting. // TODO Decoration. Like Docusaurus builtin, or https://github.com/shikijs/shiki/issues/5 // or https://github.com/andrewbranch/gatsby-remark-vscode#line-highlighting -const createHighlighter = ({ theme = "nord" } = {}) => { +const createHighlighter = ({ themes = ["nord"] } = {}) => { // Reuse the same instance - const highlighterPromise = getHighlighter({ theme }).then( + const highlighterPromise = getHighlighter({ themes }).then( async (highlighter) => { const langs = [ // See languages/README.md for sources. @@ -65,64 +65,70 @@ const createHighlighter = ({ theme = "nord" } = {}) => { return () => async (tree) => { const highlighter = await highlighterPromise; - const fg = highlighter.getForegroundColor(); - const bg = highlighter.getBackgroundColor(); visit(tree, "code", (node) => { - // const meta = node.meta; - const lang = node.lang || "text"; - const code = node.value; - let tokenLines = null; - try { - tokenLines = highlighter.codeToThemedTokens( - code, - remapLanguageId(lang) - ); - } catch (e) { - if (/^No language registration for/.test(e.message)) { - console.warn(`shiki: ${e.message}`); - console.warn(`shiki: Falling back to "text"`); - tokenLines = highlighter.codeToThemedTokens(code, "text"); - } else { - throw e; - } - } - - let html = `
`;
-      html += ``;
-      for (const line of tokenLines) {
-        html += ``;
-        for (const token of line) {
-          const css = [`color: ${token.color || fg}`];
-          switch (token.fontStyle) {
-            case -1: // NotSet
-            case 0: // None
-              break;
-            case 1: // Italic
-              css.push(`font-style: italic`);
-              break;
-            case 2: // Bold
-              css.push(`font-weight: bold`);
-              break;
-            case 4: // Underline
-              css.push(`text-decoration: underline`);
-              break;
-          }
-          const content = escapeHtml(token.content);
-          html += `${content}`;
-        }
-        html += `\n`;
-      }
-
-      html = html.replace(/\n*$/, ""); // get rid of trailing new lines
-      html += `
`; - - node.value = html; + node.value = themes + .map((theme) => + codeToHtml(highlighter, node.value, node.lang || "text", theme) + ) + .join(""); node.type = "html"; }); }; }; +const codeToHtml = (highlighter, code, lang, theme) => { + let tokenLines = null; + try { + tokenLines = highlighter.codeToThemedTokens( + code, + remapLanguageId(lang), + theme + ); + } catch (e) { + if (/^No language registration for/.test(e.message)) { + console.warn(`shiki: ${e.message}`); + console.warn(`shiki: Falling back to "text"`); + tokenLines = highlighter.codeToThemedTokens(code, "text", theme); + } else { + throw e; + } + } + + const fg = highlighter.getForegroundColor(theme); + const bg = highlighter.getBackgroundColor(theme); + + let html = `
`;
+  html += ``;
+  for (const line of tokenLines) {
+    html += ``;
+    for (const token of line) {
+      const css = [`color: ${token.color || fg}`];
+      switch (token.fontStyle) {
+        case -1: // NotSet
+        case 0: // None
+          break;
+        case 1: // Italic
+          css.push(`font-style: italic`);
+          break;
+        case 2: // Bold
+          css.push(`font-weight: bold`);
+          break;
+        case 4: // Underline
+          css.push(`text-decoration: underline`);
+          break;
+      }
+      const content = escapeHtml(token.content);
+      html += `${content}`;
+    }
+    html += `\n`;
+  }
+
+  html = html.replace(/\n*$/, ""); // get rid of trailing new lines
+  html += `
`; + return html; +}; + module.exports = { createHighlighter }; const HTML_ESCAPES = Object.freeze({