@otto-code/highlight 0.7.5 → 0.7.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/detect.d.ts CHANGED
@@ -1,2 +1,12 @@
1
+ /**
2
+ * Every extension `detectLanguage` can return.
3
+ *
4
+ * Exported so the invariant in this file's header — that each one is a key in
5
+ * parsers.ts's table — can actually be tested. It stopped being true once
6
+ * before: the v0.2.5 merge dropped the shell and SQL rows from that table while
7
+ * these rules kept scoring both, so detection claimed a language the renderer
8
+ * could not colour.
9
+ */
10
+ export declare const DETECTABLE_EXTENSIONS: readonly string[];
1
11
  export declare function detectLanguage(code: string): string | null;
2
12
  //# sourceMappingURL=detect.d.ts.map
package/dist/detect.js CHANGED
@@ -107,6 +107,16 @@ function scoreRules(sample) {
107
107
  }
108
108
  return scores;
109
109
  }
110
+ /**
111
+ * Every extension `detectLanguage` can return.
112
+ *
113
+ * Exported so the invariant in this file's header — that each one is a key in
114
+ * parsers.ts's table — can actually be tested. It stopped being true once
115
+ * before: the v0.2.5 merge dropped the shell and SQL rows from that table while
116
+ * these rules kept scoring both, so detection claimed a language the renderer
117
+ * could not colour.
118
+ */
119
+ export const DETECTABLE_EXTENSIONS = [...new Set(RULES.map((rule) => rule.ext))];
110
120
  // Best-effort guess of the language extension for a code block, or null when
111
121
  // there isn't enough signal to be confident.
112
122
  export function detectLanguage(code) {
@@ -1,48 +1,6 @@
1
- import { highlightTree, tagHighlighter, tags } from "@lezer/highlight";
1
+ import { highlightTree } from "@lezer/highlight";
2
2
  import { getParserForFile } from "./parsers.js";
3
- const highlighter = tagHighlighter([
4
- { tag: tags.keyword, class: "keyword" },
5
- { tag: tags.controlKeyword, class: "keyword" },
6
- { tag: tags.operatorKeyword, class: "keyword" },
7
- { tag: tags.definitionKeyword, class: "keyword" },
8
- { tag: tags.moduleKeyword, class: "keyword" },
9
- { tag: tags.comment, class: "comment" },
10
- { tag: tags.lineComment, class: "comment" },
11
- { tag: tags.blockComment, class: "comment" },
12
- { tag: tags.docComment, class: "comment" },
13
- { tag: tags.string, class: "string" },
14
- { tag: tags.special(tags.string), class: "string" },
15
- { tag: tags.number, class: "number" },
16
- { tag: tags.integer, class: "number" },
17
- { tag: tags.float, class: "number" },
18
- { tag: tags.bool, class: "literal" },
19
- { tag: tags.null, class: "literal" },
20
- { tag: tags.function(tags.variableName), class: "function" },
21
- { tag: tags.function(tags.propertyName), class: "function" },
22
- { tag: tags.definition(tags.variableName), class: "definition" },
23
- { tag: tags.definition(tags.propertyName), class: "definition" },
24
- { tag: tags.definition(tags.function(tags.variableName)), class: "definition" },
25
- { tag: tags.className, class: "class" },
26
- { tag: tags.definition(tags.className), class: "class" },
27
- { tag: tags.typeName, class: "type" },
28
- { tag: tags.tagName, class: "tag" },
29
- { tag: tags.attributeName, class: "attribute" },
30
- { tag: tags.attributeValue, class: "string" },
31
- { tag: tags.propertyName, class: "property" },
32
- { tag: tags.variableName, class: "variable" },
33
- { tag: tags.local(tags.variableName), class: "variable" },
34
- { tag: tags.special(tags.variableName), class: "variable" },
35
- { tag: tags.operator, class: "operator" },
36
- { tag: tags.punctuation, class: "punctuation" },
37
- { tag: tags.bracket, class: "punctuation" },
38
- { tag: tags.separator, class: "punctuation" },
39
- { tag: tags.regexp, class: "regexp" },
40
- { tag: tags.escape, class: "escape" },
41
- { tag: tags.meta, class: "meta" },
42
- { tag: tags.heading, class: "heading" },
43
- { tag: tags.link, class: "link" },
44
- { tag: tags.url, class: "link" },
45
- ]);
3
+ import { staticSyntaxHighlighter } from "./syntax-roles.js";
46
4
  export function highlightCode(code, filename) {
47
5
  const parser = getParserForFile(filename);
48
6
  if (!parser) {
@@ -56,7 +14,7 @@ export function highlightCode(code, filename) {
56
14
  }
57
15
  // Build a map of character positions to styles
58
16
  const styleMap = Array.from({ length: code.length }, () => null);
59
- highlightTree(tree, highlighter, (from, to, classes) => {
17
+ highlightTree(tree, staticSyntaxHighlighter, (from, to, classes) => {
60
18
  for (let i = from; i < to && i < styleMap.length; i++) {
61
19
  styleMap[i] = classes;
62
20
  }
package/dist/index.d.ts CHANGED
@@ -1,10 +1,13 @@
1
1
  export type { HighlightStyle, HighlightToken } from "./types.js";
2
- export { getParserForFile, isLanguageSupported, getSupportedExtensions } from "./parsers.js";
2
+ export { getLanguageForFile, getParserForFile, isLanguageSupported, getSupportedExtensions, } from "./parsers.js";
3
+ export { createCodeMirrorHighlightStyle } from "./syntax-roles.js";
3
4
  export { getLanguageDisplayName } from "./language-names.js";
4
5
  export { highlightCode, highlightLine } from "./highlighter.js";
5
6
  export { detectLanguage } from "./detect.js";
6
7
  export { extractSymbols } from "./symbols.js";
7
8
  export type { CodeSymbol, SymbolKind } from "./symbols.js";
9
+ export { extractMarkdownHeadings } from "./markdown-headings.js";
10
+ export type { MarkdownHeading } from "./markdown-headings.js";
8
11
  export { darkHighlightColors, lightHighlightColors } from "./colors.js";
9
12
  export type { SyntaxThemeId, SyntaxThemeOption, SyntaxColors } from "./themes.js";
10
13
  export { SYNTAX_THEME_IDS, SYNTAX_THEME_OPTIONS, isSyntaxThemeId, resolveSyntaxColors, } from "./themes.js";
package/dist/index.js CHANGED
@@ -1,8 +1,10 @@
1
- export { getParserForFile, isLanguageSupported, getSupportedExtensions } from "./parsers.js";
1
+ export { getLanguageForFile, getParserForFile, isLanguageSupported, getSupportedExtensions, } from "./parsers.js";
2
+ export { createCodeMirrorHighlightStyle } from "./syntax-roles.js";
2
3
  export { getLanguageDisplayName } from "./language-names.js";
3
4
  export { highlightCode, highlightLine } from "./highlighter.js";
4
5
  export { detectLanguage } from "./detect.js";
5
6
  export { extractSymbols } from "./symbols.js";
7
+ export { extractMarkdownHeadings } from "./markdown-headings.js";
6
8
  export { darkHighlightColors, lightHighlightColors } from "./colors.js";
7
9
  export { SYNTAX_THEME_IDS, SYNTAX_THEME_OPTIONS, isSyntaxThemeId, resolveSyntaxColors, } from "./themes.js";
8
10
  //# sourceMappingURL=index.js.map
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Markdown headings, for the document outline and for link autocompletion
3
+ * against a document's own anchors.
4
+ *
5
+ * **This is deliberately not part of `extractSymbols`.** A heading is not a
6
+ * `SymbolKind`, and adding one would mean adding a value to
7
+ * `CodeSymbolKindSchema` — a `z.enum` on the wire. A six-month-old client
8
+ * parsing a `code.outline.response` carrying `kind: "heading"` would reject the
9
+ * whole message, which the protocol contract forbids. Headings therefore stay
10
+ * **client-side**: the client already holds the document it wants an outline of,
11
+ * so there is nothing to ask the daemon for. That also lets a heading carry its
12
+ * level, which the flat symbol shape cannot express and a real table of contents
13
+ * needs.
14
+ */
15
+ export interface MarkdownHeading {
16
+ /** 1 through 6, from the marker count (ATX) or the underline character (Setext). */
17
+ level: number;
18
+ /**
19
+ * The heading text with its markers removed. Inline markup is left exactly as
20
+ * written: an outline row showing `**Setup**` is honest about the source, and
21
+ * stripping emphasis here would mean a second, partial markdown parser.
22
+ */
23
+ text: string;
24
+ /** 1-based line the heading starts on. */
25
+ line: number;
26
+ /** 0-based offset of the heading node, for scrolling straight to it. */
27
+ from: number;
28
+ }
29
+ /**
30
+ * Extract the headings of a markdown document, in document order.
31
+ *
32
+ * This parses rather than scanning for `#` because only a parse knows that a
33
+ * `#` opening a line inside a fenced code block is a comment, not a heading —
34
+ * the single most common way a regex-based outline goes wrong on a README.
35
+ */
36
+ export declare function extractMarkdownHeadings(code: string): MarkdownHeading[];
37
+ //# sourceMappingURL=markdown-headings.d.ts.map
@@ -0,0 +1,64 @@
1
+ import { parser as markdownParser } from "@lezer/markdown";
2
+ const HEADING_NODE = /^(?:ATX|Setext)Heading([1-6])$/;
3
+ // `## Title ##` — the leading run, and the optional closing run ATX allows.
4
+ // The closing run must be preceded by whitespace, per CommonMark, which is what
5
+ // keeps `# C#` from becoming "C".
6
+ const ATX_LEADING = /^[ \t]*#{1,6}[ \t]*/;
7
+ const ATX_TRAILING = /\s#+[ \t]*$/;
8
+ function buildLineStarts(code) {
9
+ const starts = [0];
10
+ for (let index = 0; index < code.length; index += 1) {
11
+ if (code.charCodeAt(index) === 10) {
12
+ starts.push(index + 1);
13
+ }
14
+ }
15
+ return starts;
16
+ }
17
+ // Binary search for the 0-based line index whose start is <= offset.
18
+ function lineIndexForOffset(lineStarts, offset) {
19
+ let low = 0;
20
+ let high = lineStarts.length - 1;
21
+ while (low < high) {
22
+ const mid = (low + high + 1) >> 1;
23
+ if (lineStarts[mid] <= offset) {
24
+ low = mid;
25
+ }
26
+ else {
27
+ high = mid - 1;
28
+ }
29
+ }
30
+ return low;
31
+ }
32
+ /**
33
+ * Extract the headings of a markdown document, in document order.
34
+ *
35
+ * This parses rather than scanning for `#` because only a parse knows that a
36
+ * `#` opening a line inside a fenced code block is a comment, not a heading —
37
+ * the single most common way a regex-based outline goes wrong on a README.
38
+ */
39
+ export function extractMarkdownHeadings(code) {
40
+ const tree = markdownParser.parse(code);
41
+ const lineStarts = buildLineStarts(code);
42
+ const headings = [];
43
+ const cursor = tree.cursor();
44
+ do {
45
+ const match = HEADING_NODE.exec(cursor.name);
46
+ if (!match) {
47
+ continue;
48
+ }
49
+ const level = Number(match[1]);
50
+ const raw = code.slice(cursor.from, cursor.to);
51
+ // A Setext heading spans its text line and its underline; the underline is
52
+ // the marker, so only the first line is the text.
53
+ const firstLine = raw.split("\n", 1)[0] ?? "";
54
+ const text = firstLine.replace(ATX_LEADING, "").replace(ATX_TRAILING, "").trim();
55
+ headings.push({
56
+ level,
57
+ text,
58
+ line: lineIndexForOffset(lineStarts, cursor.from) + 1,
59
+ from: cursor.from,
60
+ });
61
+ } while (cursor.next());
62
+ return headings;
63
+ }
64
+ //# sourceMappingURL=markdown-headings.js.map
package/dist/parsers.d.ts CHANGED
@@ -1,4 +1,6 @@
1
+ import { Language } from "@codemirror/language";
1
2
  import type { Parser } from "@lezer/common";
3
+ export declare function getLanguageForFile(filename: string): Language | null;
2
4
  export declare function getParserForFile(filename: string): Parser | null;
3
5
  export declare function isLanguageSupported(filename: string): boolean;
4
6
  export declare function getSupportedExtensions(): string[];
package/dist/parsers.js CHANGED
@@ -1,8 +1,8 @@
1
- import { StreamLanguage } from "@codemirror/language";
1
+ import { defineLanguageFacet, Language, StreamLanguage } from "@codemirror/language";
2
2
  import { dart } from "@codemirror/legacy-modes/mode/clike";
3
- import { swift } from "@codemirror/legacy-modes/mode/swift";
4
3
  import { shell } from "@codemirror/legacy-modes/mode/shell";
5
4
  import { standardSQL } from "@codemirror/legacy-modes/mode/sql";
5
+ import { swift } from "@codemirror/legacy-modes/mode/swift";
6
6
  import { parser as jsParser } from "@lezer/javascript";
7
7
  import { parser as jsonParser } from "@lezer/json";
8
8
  import { parser as cssParser } from "@lezer/css";
@@ -18,79 +18,90 @@ import { parser as xmlParser } from "@lezer/xml";
18
18
  import { parser as yamlParser } from "@lezer/yaml";
19
19
  import { csharpLanguage } from "@replit/codemirror-lang-csharp";
20
20
  import { parser as elixirParser } from "lezer-elixir";
21
+ function language(parser) {
22
+ return new Language(defineLanguageFacet(), parser);
23
+ }
21
24
  // Shared instance so the four shell fence aliases don't each build a grammar.
22
- const shellParser = StreamLanguage.define(shell).parser;
23
- const parsersByExtension = {
25
+ const shellLanguage = StreamLanguage.define(shell);
26
+ const languagesByExtension = {
24
27
  // JavaScript/TypeScript
25
- js: jsParser,
26
- jsx: jsParser.configure({ dialect: "jsx" }),
27
- ts: jsParser.configure({ dialect: "ts" }),
28
- tsx: jsParser.configure({ dialect: "ts jsx" }),
29
- mjs: jsParser,
30
- cjs: jsParser,
28
+ js: language(jsParser),
29
+ jsx: language(jsParser.configure({ dialect: "jsx" })),
30
+ ts: language(jsParser.configure({ dialect: "ts" })),
31
+ tsx: language(jsParser.configure({ dialect: "ts jsx" })),
32
+ mjs: language(jsParser),
33
+ cjs: language(jsParser),
31
34
  // C / C++ / Objective-C
32
- c: cppParser,
33
- h: cppParser,
34
- cc: cppParser,
35
- cpp: cppParser,
36
- cxx: cppParser,
37
- hpp: cppParser,
38
- hxx: cppParser,
39
- m: cppParser,
40
- mm: cppParser,
35
+ c: language(cppParser),
36
+ h: language(cppParser),
37
+ cc: language(cppParser),
38
+ cpp: language(cppParser),
39
+ cxx: language(cppParser),
40
+ hpp: language(cppParser),
41
+ hxx: language(cppParser),
42
+ m: language(cppParser),
43
+ mm: language(cppParser),
41
44
  // JSON
42
- json: jsonParser,
45
+ json: language(jsonParser),
43
46
  // CSS
44
- css: cssParser,
45
- scss: cssParser,
47
+ css: language(cssParser),
48
+ scss: language(cssParser),
46
49
  // HTML
47
- html: htmlParser,
48
- htm: htmlParser,
50
+ html: language(htmlParser),
51
+ htm: language(htmlParser),
49
52
  // XML
50
- xml: xmlParser,
53
+ xml: language(xmlParser),
51
54
  // Java
52
- java: javaParser,
55
+ java: language(javaParser),
53
56
  // Python
54
- py: pythonParser,
57
+ py: language(pythonParser),
55
58
  // Go
56
- go: goParser,
59
+ go: language(goParser),
57
60
  // PHP
58
- php: phpParser,
61
+ php: language(phpParser),
59
62
  // YAML
60
- yaml: yamlParser,
61
- yml: yamlParser,
63
+ yaml: language(yamlParser),
64
+ yml: language(yamlParser),
62
65
  // Rust
63
- rs: rustParser,
66
+ rs: language(rustParser),
64
67
  // Swift
65
- swift: StreamLanguage.define(swift).parser,
68
+ swift: StreamLanguage.define(swift),
66
69
  // Dart
67
- dart: StreamLanguage.define(dart).parser,
68
- // Shell (fence tags bash/sh/zsh/shell all map here)
69
- sh: shellParser,
70
- bash: shellParser,
71
- zsh: shellParser,
72
- shell: shellParser,
73
- // SQL
74
- sql: StreamLanguage.define(standardSQL).parser,
70
+ dart: StreamLanguage.define(dart),
75
71
  // C#
76
- cs: csharpLanguage.parser,
72
+ cs: csharpLanguage,
77
73
  // Elixir
78
- ex: elixirParser,
79
- exs: elixirParser,
74
+ ex: language(elixirParser),
75
+ exs: language(elixirParser),
80
76
  // Markdown
81
- md: markdownParser,
82
- mdx: markdownParser,
77
+ md: language(markdownParser),
78
+ mdx: language(markdownParser),
79
+ // Shell. `detect.ts` has scored shell since it was written and can return
80
+ // "sh", so without these rows we classified a snippet as shell and then had
81
+ // nothing to colour it with. Lost in the Paseo v0.2.5 merge when this table
82
+ // took upstream's shape; see projects/paseo-v025-merge/audit-findings.md.
83
+ // All four fence tags (sh/bash/zsh/shell) map here.
84
+ sh: shellLanguage,
85
+ bash: shellLanguage,
86
+ zsh: shellLanguage,
87
+ shell: shellLanguage,
88
+ // SQL. Same story: "sql" is a detect.ts verdict. `standardSQL` is the dialect
89
+ // -neutral mode, which is the right default for snippets of unknown origin.
90
+ sql: StreamLanguage.define(standardSQL),
83
91
  };
84
- export function getParserForFile(filename) {
92
+ export function getLanguageForFile(filename) {
85
93
  const ext = filename.split(".").pop()?.toLowerCase();
86
94
  if (!ext)
87
95
  return null;
88
- return parsersByExtension[ext] ?? null;
96
+ return languagesByExtension[ext] ?? null;
97
+ }
98
+ export function getParserForFile(filename) {
99
+ return getLanguageForFile(filename)?.parser ?? null;
89
100
  }
90
101
  export function isLanguageSupported(filename) {
91
102
  return getParserForFile(filename) !== null;
92
103
  }
93
104
  export function getSupportedExtensions() {
94
- return Object.keys(parsersByExtension);
105
+ return Object.keys(languagesByExtension);
95
106
  }
96
107
  //# sourceMappingURL=parsers.js.map
@@ -0,0 +1,10 @@
1
+ import { HighlightStyle as CodeMirrorHighlightStyle } from "@codemirror/language";
2
+ import { type Tag } from "@lezer/highlight";
3
+ import type { HighlightStyle } from "./types.js";
4
+ export declare const syntaxRoleTags: ReadonlyArray<{
5
+ tag: Tag;
6
+ role: HighlightStyle;
7
+ }>;
8
+ export declare const staticSyntaxHighlighter: import("@lezer/highlight").Highlighter;
9
+ export declare function createCodeMirrorHighlightStyle(colors: Record<HighlightStyle, string>): CodeMirrorHighlightStyle;
10
+ //# sourceMappingURL=syntax-roles.d.ts.map
@@ -0,0 +1,50 @@
1
+ import { HighlightStyle as CodeMirrorHighlightStyle } from "@codemirror/language";
2
+ import { tagHighlighter, tags } from "@lezer/highlight";
3
+ export const syntaxRoleTags = [
4
+ { tag: tags.keyword, role: "keyword" },
5
+ { tag: tags.controlKeyword, role: "keyword" },
6
+ { tag: tags.operatorKeyword, role: "keyword" },
7
+ { tag: tags.definitionKeyword, role: "keyword" },
8
+ { tag: tags.moduleKeyword, role: "keyword" },
9
+ { tag: tags.comment, role: "comment" },
10
+ { tag: tags.lineComment, role: "comment" },
11
+ { tag: tags.blockComment, role: "comment" },
12
+ { tag: tags.docComment, role: "comment" },
13
+ { tag: tags.string, role: "string" },
14
+ { tag: tags.special(tags.string), role: "string" },
15
+ { tag: tags.number, role: "number" },
16
+ { tag: tags.integer, role: "number" },
17
+ { tag: tags.float, role: "number" },
18
+ { tag: tags.bool, role: "literal" },
19
+ { tag: tags.null, role: "literal" },
20
+ { tag: tags.function(tags.variableName), role: "function" },
21
+ { tag: tags.function(tags.propertyName), role: "function" },
22
+ { tag: tags.definition(tags.variableName), role: "definition" },
23
+ { tag: tags.definition(tags.propertyName), role: "definition" },
24
+ { tag: tags.definition(tags.function(tags.variableName)), role: "definition" },
25
+ { tag: tags.className, role: "class" },
26
+ { tag: tags.definition(tags.className), role: "class" },
27
+ { tag: tags.typeName, role: "type" },
28
+ { tag: tags.tagName, role: "tag" },
29
+ { tag: tags.attributeName, role: "attribute" },
30
+ { tag: tags.attributeValue, role: "string" },
31
+ { tag: tags.propertyName, role: "property" },
32
+ { tag: tags.variableName, role: "variable" },
33
+ { tag: tags.local(tags.variableName), role: "variable" },
34
+ { tag: tags.special(tags.variableName), role: "variable" },
35
+ { tag: tags.operator, role: "operator" },
36
+ { tag: tags.punctuation, role: "punctuation" },
37
+ { tag: tags.bracket, role: "punctuation" },
38
+ { tag: tags.separator, role: "punctuation" },
39
+ { tag: tags.regexp, role: "regexp" },
40
+ { tag: tags.escape, role: "escape" },
41
+ { tag: tags.meta, role: "meta" },
42
+ { tag: tags.heading, role: "heading" },
43
+ { tag: tags.link, role: "link" },
44
+ { tag: tags.url, role: "link" },
45
+ ];
46
+ export const staticSyntaxHighlighter = tagHighlighter(syntaxRoleTags.map(({ tag, role }) => ({ tag, class: role })));
47
+ export function createCodeMirrorHighlightStyle(colors) {
48
+ return CodeMirrorHighlightStyle.define(syntaxRoleTags.map(({ tag, role }) => ({ tag, color: colors[role] })));
49
+ }
50
+ //# sourceMappingURL=syntax-roles.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@otto-code/highlight",
3
- "version": "0.7.5",
3
+ "version": "0.7.6",
4
4
  "license": "AGPL-3.0-or-later",
5
5
  "files": [
6
6
  "dist",
@@ -28,7 +28,7 @@
28
28
  "typecheck": "tsgo --noEmit"
29
29
  },
30
30
  "dependencies": {
31
- "@codemirror/language": "^6.12.3",
31
+ "@codemirror/language": "6.12.4",
32
32
  "@codemirror/legacy-modes": "^6.5.3",
33
33
  "@lezer/common": "^1.5.0",
34
34
  "@lezer/cpp": "^1.1.5",