diff --git a/docs/docset.yml b/docs/docset.yml index 635e68457..b7bb07219 100644 --- a/docs/docset.yml +++ b/docs/docset.yml @@ -117,6 +117,7 @@ toc: - file: req.md - folder: nested - file: cross-links.md + - file: custom-highlighters.md - folder: mover children: - file: first-page.md diff --git a/docs/testing/custom-highlighters.md b/docs/testing/custom-highlighters.md new file mode 100644 index 000000000..cde141e91 --- /dev/null +++ b/docs/testing/custom-highlighters.md @@ -0,0 +1,71 @@ +# Additional syntax highlighters + + +## Console / REST API documentation + +::::{tab-set} + +:::{tab-item} Output + +```console +GET /mydocuments/_search +{ + "from": 1, + "query": { + "match_all" {} + } +} +``` + +::: + +:::{tab-item} Markdown + +````markdown +```console +GET /mydocuments/_search +{ + "from": 1, + "query": { + "match_all" {} + } +} +``` +```` + +## EQL + +sequence +```eql +sequence + [ file where file.extension == "exe" ] + [ process where true ] +``` + +sequence until + +```eql +sequence by ID + A + B +until C +``` +sample + +```eql +sample by host + [ file where file.extension == "exe" ] + [ process where true ] +``` +head (pipes) +```eql +process where process.name == "svchost.exe" +| tail 5 +``` +function calls + +```eql +modulo(10, 6) +modulo(10, 5) +modulo(10, 0.5) +``` \ No newline at end of file diff --git a/src/Elastic.Markdown/Assets/hljs.ts b/src/Elastic.Markdown/Assets/hljs.ts index 7e4c9c4da..f00895a79 100644 --- a/src/Elastic.Markdown/Assets/hljs.ts +++ b/src/Elastic.Markdown/Assets/hljs.ts @@ -14,8 +14,63 @@ hljs.registerLanguage('apiheader', function() { ], } }) -hljs.addPlugin(mergeHTMLPlugin); +// https://tc39.es/ecma262/#sec-literals-numeric-literals +const decimalDigits = '[0-9](_?[0-9])*'; +const frac = `\\.(${decimalDigits})`; +// DecimalIntegerLiteral, including Annex B NonOctalDecimalIntegerLiteral +// https://tc39.es/ecma262/#sec-additional-syntax-numeric-literals +const decimalInteger = `0|[1-9](_?[0-9])*|0[0-7]*[89][0-9]*`; +const NUMBER = { + className: 'number', + variants: [ + // DecimalLiteral + { begin: `(\\b(${decimalInteger})((${frac})|\\.)?|(${frac}))` + + `[eE][+-]?(${decimalDigits})\\b` }, + { begin: `\\b(${decimalInteger})\\b((${frac})\\b|\\.)?|(${frac})\\b` }, + + // DecimalBigIntegerLiteral + { begin: `\\b(0|[1-9](_?[0-9])*)n\\b` }, + + // NonDecimalIntegerLiteral + { begin: "\\b0[xX][0-9a-fA-F](_?[0-9a-fA-F])*n?\\b" }, + { begin: "\\b0[bB][0-1](_?[0-1])*n?\\b" }, + { begin: "\\b0[oO][0-7](_?[0-7])*n?\\b" }, + + // LegacyOctalIntegerLiteral (does not include underscore separators) + // https://tc39.es/ecma262/#sec-additional-syntax-numeric-literals + { begin: "\\b0[0-7]+n?\\b" }, + ], + relevance: 0 +}; + +hljs.registerLanguage('eql', function() { + return { + case_insensitive: true, // language is case-insensitive + keywords: { + keyword: 'where sequence sample untill and or not in in~', + literal: ['false','true','null'], + 'subst': 'add between cidrMatch concat divide endsWith indexOf length modulo multiply number startsWith string stringContains substring subtract' + }, + contains: [ + hljs.QUOTE_STRING_MODE, + hljs.C_LINE_COMMENT_MODE, + { + scope: "operator", // (pathname: path1/path2/dothis) color #ab5656 + match: /(?:<|<=|==|:|!=|>=|>|like~?|regex~?)/, + }, + { + scope: "punctuation", // (pathname: path1/path2/dothis) color #ab5656 + match: /(?:!?\[|\]|\|)/, + }, + NUMBER, + + ] + } +}) + +hljs.addPlugin(mergeHTMLPlugin); export function initHighlight() { + hljs.highlightAll(); } diff --git a/src/Elastic.Markdown/Assets/toc-nav.ts b/src/Elastic.Markdown/Assets/toc-nav.ts index 20b55fa09..95ee4f238 100644 --- a/src/Elastic.Markdown/Assets/toc-nav.ts +++ b/src/Elastic.Markdown/Assets/toc-nav.ts @@ -128,8 +128,10 @@ function setupSmoothScrolling(elements: TocElements) { export function initTocNav() { const elements = initializeTocElements(); - elements.progressIndicator.style.height = '0'; - elements.progressIndicator.style.top = '0'; + if (elements.progressIndicator != null) { + elements.progressIndicator.style.height = '0'; + elements.progressIndicator.style.top = '0'; + } const update = () => updateIndicator(elements) update(); window.addEventListener('scroll', update); diff --git a/src/Elastic.Markdown/Myst/CodeBlocks/SupportedLanguages.cs b/src/Elastic.Markdown/Myst/CodeBlocks/SupportedLanguages.cs index 68d7ca45e..38e3ff95f 100644 --- a/src/Elastic.Markdown/Myst/CodeBlocks/SupportedLanguages.cs +++ b/src/Elastic.Markdown/Myst/CodeBlocks/SupportedLanguages.cs @@ -179,6 +179,9 @@ public static class CodeBlock { "xquery", "xpath, xq, xqm" }, // XQuery { "yml", "yaml" }, // YAML { "zephir", "zep" }, // Zephir + + //CUSTOM, Elastic language we wrote highlighters for + { "eql", "" } }; diff --git a/src/Elastic.Markdown/Slices/Layout/_Scripts.cshtml b/src/Elastic.Markdown/Slices/Layout/_Scripts.cshtml index a37818726..1d625db25 100644 --- a/src/Elastic.Markdown/Slices/Layout/_Scripts.cshtml +++ b/src/Elastic.Markdown/Slices/Layout/_Scripts.cshtml @@ -18,5 +18,4 @@ - diff --git a/src/Elastic.Markdown/_static/custom.js b/src/Elastic.Markdown/_static/custom.js deleted file mode 100644 index 17388341c..000000000 --- a/src/Elastic.Markdown/_static/custom.js +++ /dev/null @@ -1,12 +0,0 @@ -hljs.registerLanguage('apiheader', function() { - return { - case_insensitive: true, // language is case-insensitive - keywords: 'GET POST PUT DELETE HEAD OPTIONS PATCH', - contains: [ - hljs.HASH_COMMENT_MODE, - { - className: "subst", // (pathname: path1/path2/dothis) color #ab5656 - begin: /(?<=(?:\/|GET |POST |PUT |DELETE |HEAD |OPTIONS |PATH))[^?\n\r\/]+/, - } - ], } -})