highlightjs / highlight.js

JavaScript syntax highlighter with language auto-detection and zero dependencies.
https://highlightjs.org/
BSD 3-Clause "New" or "Revised" License
23.3k stars 3.52k forks source link

Fix/issue 3972 #3989

Closed akhtarmdsaad closed 4 months ago

akhtarmdsaad commented 4 months ago

Problem: When I use namespace as the name of the variable, everything that follows is highlighted incorrectly. While namespace is a TS keyword it's fine to use it as a variable name too. Solution: Used a multimatcher to solve it

Before:

image

After:

image

Resolves #3972

Changes

Before:

const NAMESPACE = {
    beginKeywords: 'namespace',
    end: /\{/,
    excludeEnd: true,
    contains: [ tsLanguage.exports.CLASS_REFERENCE ]
  };

After:

const NAMESPACE = {
    begin: [
      /namespace/,
      /\s+/,
      hljs.IDENT_RE
    ],
    beginScope: {
      1: "keyword",
      3: "variable"
    },
    end: /\{/,
    excludeEnd: true,
    contains: [tsLanguage.exports.CLASS_REFERENCE]
  };

Checklist

github-actions[bot] commented 4 months ago

Build Size Report

Changes to minified artifacts in /build, after gzip compression.

5 files changed

Total change +40 B

View Changes | file | base | pr | diff | | --- | --- | --- | --- | | es/core.min.js | 8.17 KB | 8.17 KB | -1 B | | es/highlight.min.js | 8.17 KB | 8.17 KB | -1 B | | es/languages/typescript.min.js | 3.15 KB | 3.17 KB | +22 B | | highlight.min.js | 8.21 KB | 8.2 KB | -2 B | | languages/typescript.min.js | 3.16 KB | 3.18 KB | +22 B |
github-actions[bot] commented 4 months ago

Build Size Report

Changes to minified artifacts in /build, after gzip compression.

5 files changed

Total change +31 B

View Changes | file | base | pr | diff | | --- | --- | --- | --- | | es/core.min.js | 8.17 KB | 8.18 KB | +2 B | | es/highlight.min.js | 8.17 KB | 8.18 KB | +2 B | | es/languages/typescript.min.js | 3.15 KB | 3.16 KB | +12 B | | highlight.min.js | 8.21 KB | 8.21 KB | +2 B | | languages/typescript.min.js | 3.16 KB | 3.17 KB | +13 B |
github-actions[bot] commented 4 months ago

Build Size Report

Changes to minified artifacts in /build, after gzip compression.

3 files changed

Total change +24 B

View Changes | file | base | pr | diff | | --- | --- | --- | --- | | es/languages/typescript.min.js | 3.15 KB | 3.16 KB | +12 B | | highlight.min.js | 8.21 KB | 8.21 KB | -1 B | | languages/typescript.min.js | 3.16 KB | 3.17 KB | +13 B |
github-actions[bot] commented 4 months ago

Build Size Report

Changes to minified artifacts in /build, after gzip compression.

3 files changed

Total change +12 B

View Changes | file | base | pr | diff | | --- | --- | --- | --- | | es/languages/typescript.min.js | 3.15 KB | 3.16 KB | +6 B | | highlight.min.js | 8.21 KB | 8.21 KB | -1 B | | languages/typescript.min.js | 3.16 KB | 3.17 KB | +7 B |
github-actions[bot] commented 4 months ago

Build Size Report

Changes to minified artifacts in /build, after gzip compression.

3 files changed

Total change +12 B

View Changes | file | base | pr | diff | | --- | --- | --- | --- | | es/languages/typescript.min.js | 3.15 KB | 3.16 KB | +6 B | | highlight.min.js | 8.21 KB | 8.21 KB | -1 B | | languages/typescript.min.js | 3.16 KB | 3.17 KB | +7 B |