Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
110 commits
Select commit Hold shift + click to select a range
3beff85
[WIP] Simplify core
LeaVerou Apr 26, 2025
46806b6
highlightAll
LeaVerou Apr 26, 2025
e4f6634
Just use properties on env, not a separate `state` property
LeaVerou Apr 26, 2025
183817c
Move singleton, eliminate state further
LeaVerou Apr 26, 2025
d1097d8
Finish removing HookState
LeaVerou Apr 26, 2025
e3e5db2
Update prism-class.ts
LeaVerou Apr 26, 2025
c866261
Delete hook-state.ts
LeaVerou Apr 26, 2025
8ce6c69
Move more functions out of prism-class
LeaVerou Apr 26, 2025
1a2c60b
Merge branch 'v2' into simplify
LeaVerou Apr 26, 2025
52fca5d
Move tokenize
LeaVerou Apr 26, 2025
1158aa9
Update src/core.ts
LeaVerou Apr 27, 2025
9acac36
Update src/types.d.ts
LeaVerou Apr 27, 2025
8a8bc18
Update src/global.ts
LeaVerou Apr 27, 2025
e0ca584
Update hooks.ts
LeaVerou Apr 27, 2025
2727307
Update hooks.ts
LeaVerou Apr 27, 2025
96e93ea
Update src/core/prism-class.ts
LeaVerou Apr 27, 2025
dbbb0dd
Update src/global.ts
LeaVerou Apr 27, 2025
4dfff4c
Update registry.ts
LeaVerou Apr 27, 2025
a2a5d23
Update eslint.config.mjs
LeaVerou Apr 27, 2025
1e31274
Remove known plugins
LeaVerou Apr 27, 2025
ca6df67
Update prism.ts
LeaVerou Apr 27, 2025
051e0e6
Update src/core/highlight.ts
LeaVerou Apr 27, 2025
2147cae
Update src/core/highlight.ts
LeaVerou Apr 27, 2025
aac6bf7
Merge branch 'v2' into simplify
LeaVerou Apr 27, 2025
6513275
Merge branch 'simplify' of https://github.com/PrismJS/prism into simp…
LeaVerou Apr 27, 2025
de0b0bd
Update src/plugins/keep-markup/prism-keep-markup.ts
LeaVerou Apr 27, 2025
83fad95
[build] Delete `known-plugins.d.ts`
DmitrySharabin Apr 27, 2025
b4366b2
[WIP] Make language definitions more declarative, simplify registry, …
LeaVerou Apr 28, 2025
73ef579
Merge branch 'simplify' of https://github.com/PrismJS/prism into simp…
LeaVerou Apr 28, 2025
0762142
Fixes (#3915)
LeaVerou Apr 28, 2025
60cadeb
`extends` -> `base` and a few other things
LeaVerou Apr 29, 2025
c7ce545
Fix `c`
LeaVerou Apr 29, 2025
abf6b71
Concept for language extensions
LeaVerou Apr 29, 2025
52fe2ae
Move $insertBefore and $delete to extend()
LeaVerou Apr 29, 2025
e12f6df
Separate language patches from other eval, attempt to move types to m…
LeaVerou Apr 29, 2025
42312a5
Update actionscript.ts
LeaVerou Apr 29, 2025
4d8b9ea
Update bison.ts
LeaVerou Apr 29, 2025
f997850
Type wrangling
LeaVerou Apr 29, 2025
5bf9582
Merge branch 'v2' into simplify
LeaVerou Apr 29, 2025
93bbe20
Some suggestions (#3919)
DmitrySharabin Apr 29, 2025
0e674d2
Add proper typing for the `Grammar` type (#3918)
DmitrySharabin Apr 29, 2025
107632e
Preserve context
DmitrySharabin Apr 29, 2025
bf68a19
Merge branch 'simplify' of https://github.com/PrismJS/prism into simp…
LeaVerou Apr 30, 2025
b7623b6
Move iterable helpers to separate file
LeaVerou May 1, 2025
b850074
TS
LeaVerou May 1, 2025
bc5144f
Merge branch 'v2' into simplify
LeaVerou May 1, 2025
1deb867
Update eslint.config.mjs
LeaVerou May 1, 2025
257e650
WIP, improve API around languages
LeaVerou May 1, 2025
552c9e6
First pass on transforming `php` and `php-extras` (#3922)
DmitrySharabin May 1, 2025
1c3531c
First pass on transforming ASP.NET (C#) (#3920)
DmitrySharabin May 1, 2025
269f5d4
First pass (#3921)
DmitrySharabin May 1, 2025
9a70161
Move grammar back into types
LeaVerou May 1, 2025
9ffbe35
Languages
LeaVerou May 1, 2025
6a14538
Drop tokenize and rest symbols in favor of $tokenize and $rest proper…
LeaVerou May 1, 2025
0ea3547
Update tokenize.ts
LeaVerou May 1, 2025
586bd55
Sample rewrite to show what group matching + $language can do
LeaVerou May 1, 2025
69f8bf3
Remove effect from languages
LeaVerou May 1, 2025
097f21a
Suggestions to the `simplify` branch (#3925)
DmitrySharabin May 2, 2025
6fb945c
Allow implicit any
LeaVerou May 2, 2025
f7c21f6
Move language utils to `util/` and separate them out
LeaVerou May 2, 2025
1baaebf
Add aliases to C++, C#, F# so markdown doesn't need to handle them sp…
LeaVerou May 2, 2025
b5d562e
Drop xml-doc
LeaVerou May 2, 2025
ff26b79
Drop shared file, not worth it
LeaVerou May 2, 2025
42ebc79
Some more work on dynamic langs (still very WIP)
LeaVerou May 2, 2025
9fb2ed1
`resolveGrammar` -> `grammarPatch`
LeaVerou May 2, 2025
3d760a5
insertAfter, deep keys
LeaVerou May 2, 2025
9dc1677
Make php-extras just a part of PHP
LeaVerou May 2, 2025
ba3a8b4
Add media types and extensions
LeaVerou May 2, 2025
dcf018c
Fix typo
DmitrySharabin May 2, 2025
577eb31
Some more suggestions to the `simplify` branch (#3926)
DmitrySharabin May 3, 2025
6661905
Preserve accessors when extending/copying objects
LeaVerou May 5, 2025
b76f673
Suggestions (next iteration) (#3928)
DmitrySharabin May 7, 2025
a08fd18
Simplify hooks, add type safety
LeaVerou May 7, 2025
e2d15e9
Update util.ts
LeaVerou May 7, 2025
dedc06d
WIP
LeaVerou May 7, 2025
abc87fd
Update src/core/classes/language-registry.ts
LeaVerou May 7, 2025
1b7e413
Update highlight.ts
LeaVerou May 7, 2025
c08e814
Merge branch 'simplify' of https://github.com/PrismJS/prism into simp…
LeaVerou May 7, 2025
5f1da53
Update src/core/classes/hooks.ts
LeaVerou May 7, 2025
97a28fd
Drop `$language`
LeaVerou May 13, 2025
0d0ea00
Break down `tokenize.ts` into separate modules
LeaVerou May 15, 2025
ff0a08b
Prettier
LeaVerou May 15, 2025
4a631e9
Registry -> ComponentRegistry
LeaVerou May 15, 2025
4e3f178
async util
LeaVerou May 15, 2025
c03ab64
Update eslint.config.mjs
LeaVerou May 17, 2025
7c8205a
Update async.ts
LeaVerou May 17, 2025
4a71b8d
Update async.ts
LeaVerou May 17, 2025
3e1e9c7
Update language.ts
LeaVerou May 17, 2025
2f5c194
Update language.ts
LeaVerou May 17, 2025
eb5e425
Update types.d.ts
LeaVerou May 17, 2025
f069a78
tokenize
LeaVerou May 17, 2025
1888f4a
Move Token class to classes
LeaVerou May 17, 2025
3a25f8e
First stab at functional $inside/$rest
LeaVerou May 18, 2025
630b6d4
Some suggestions to the `simplify` branch (#3939)
DmitrySharabin May 18, 2025
7a7979c
Update the `Language` class (#3941)
DmitrySharabin May 25, 2025
a1112bc
Some fixes to `tokenize` (#3942)
DmitrySharabin May 25, 2025
bc34dde
Partially fix tests (in the `simplify` branch) (#3940)
DmitrySharabin May 25, 2025
e07c8b7
Another attempt to avoid the “too deep” structure error and not fall…
DmitrySharabin May 26, 2025
ca7fc26
Update Prettier plugins (#3945)
DmitrySharabin May 27, 2025
ccbc10b
Bug fixes (the `simplify` branch) (#3943)
DmitrySharabin May 28, 2025
3935b1a
Correctly parse CLI arguments (#3949)
DmitrySharabin May 29, 2025
0725758
Make `nextTick()` work in environments other than browsers (#3952)
DmitrySharabin May 29, 2025
b86c4e0
Resolve the language if its definition is already loaded, and it is n…
DmitrySharabin May 29, 2025
830fdff
Fix the Markdown language (#3950)
DmitrySharabin May 29, 2025
5660d05
Transform languages (#3948)
DmitrySharabin May 31, 2025
37bdeeb
Fix the gradle language (#3955)
DmitrySharabin Jun 1, 2025
7c7c598
In addition to waiting for all promises to settle, handle post-hoc ad…
DmitrySharabin Jun 17, 2025
d8246d5
Integrate changes from the `v2` branch (#3982)
DmitrySharabin Jul 11, 2025
54a2dfa
Merge branch 'v2' into simplify
DmitrySharabin Jul 11, 2025
d110fa7
Fix issues after resolving conflicts
DmitrySharabin Jul 11, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/core/classes/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ export class Token {
}
}

export default Token;

/**
* A token stream is an array of strings and {@link Token Token} objects.
*
Expand Down
61 changes: 46 additions & 15 deletions src/core/tokenize/match.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Token } from '../classes/token';
import singleton from '../prism';
import { tokenize } from './tokenize';
import { resolve } from './util';
import type { GrammarToken, GrammarTokens, RegExpLike } from '../../types';
import { resolve, tokenizeByNamedGroups } from './util';
import type { GrammarToken, GrammarTokens, RegExpLike, TokenStream } from '../../types';
import type {
LinkedList,
LinkedListHeadNode,
Expand All @@ -22,11 +22,7 @@
): void {
const prism = this ?? singleton;

let restGrammar = resolve(prism.languageRegistry, grammar.$rest);
while (restGrammar) {
grammar = { ...grammar, ...restGrammar };
restGrammar = resolve(prism.languageRegistry, restGrammar.$rest);
}
grammar = resolve.call(prism, grammar);

Check failure on line 25 in src/core/tokenize/match.ts

View workflow job for this annotation

GitHub Actions / type-check

Type 'Function | Grammar | undefined' is not assignable to type 'GrammarTokens'.

for (const token in grammar) {
const tokenValue = grammar[token];
Expand All @@ -46,11 +42,21 @@

const patternObj = toGrammarToken(patterns[j]);
let { pattern, lookbehind = false, greedy = false, alias, inside } = patternObj;
const insideGrammar = resolve(prism.languageRegistry, inside);
const insideGrammar = resolve.call(prism, inside);

let flagsToAdd = '';

if (greedy && !pattern.global) {
// Without the global flag, lastIndex won't work
patternObj.pattern = pattern = RegExp(pattern.source, pattern.flags + 'g');
flagsToAdd += 'g';
}
if (pattern.source.indexOf('(?<') && pattern.hasIndices === false) {
// Has named groups, we need to be able to capture their indices
flagsToAdd += 'd';
}

if (flagsToAdd) {
patternObj.pattern = pattern = RegExp(pattern.source, pattern.flags + flagsToAdd);
}

for (
Expand Down Expand Up @@ -130,6 +136,7 @@

const from = match.index;
const matchStr = match[0];
let content: TokenStream | string = matchStr;
const before = str.slice(0, from);
const after = str.slice(from + matchStr.length);

Expand All @@ -146,13 +153,37 @@
}

tokenList.removeRange(removeFrom, removeCount);
let byGroups = match.groups ? tokenizeByNamedGroups(match) : null;

if (byGroups?.length > 1) {

Check failure on line 158 in src/core/tokenize/match.ts

View workflow job for this annotation

GitHub Actions / type-check

'byGroups.length' is possibly 'undefined'.
content = byGroups.map(arg => {

Check failure on line 159 in src/core/tokenize/match.ts

View workflow job for this annotation

GitHub Actions / type-check

'byGroups' is possibly 'null'.
let content = typeof arg === 'string' ? arg : arg.content;
let type = typeof arg === 'string' ? undefined : arg.type;

if (insideGrammar) {
let localInsideGrammar = insideGrammar[type] ?? insideGrammar;

if (typeof localInsideGrammar === 'function') {
// Late resolving
localInsideGrammar = resolve.call(
prism,
localInsideGrammar(match.groups)
);
}

content = tokenize.call(prism, content, localInsideGrammar);
}

return typeof arg === 'object' && arg.type
? new Token(arg.type, content)
: content;
});
}
else if (insideGrammar) {
content = tokenize.call(prism, content, insideGrammar);

Check failure on line 183 in src/core/tokenize/match.ts

View workflow job for this annotation

GitHub Actions / type-check

Argument of type 'string | TokenStream' is not assignable to parameter of type 'string'.
}

const wrapped = new Token(
token,
insideGrammar ? tokenize.call(prism, matchStr, insideGrammar) : matchStr,
alias,
matchStr
);
const wrapped = new Token(token, content, alias, matchStr);
currentNode = tokenList.addAfter(removeFrom, wrapped);

if (after) {
Expand Down
64 changes: 56 additions & 8 deletions src/core/tokenize/util.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,61 @@
import type { Grammar } from '../../types';
import type LanguageRegistry from '../classes/language-registry';
import { camelToKebabCase } from '../../shared/util';
import singleton from '../prism';
import type { Grammar, Prism } from '../../types';

export function resolve (
languageRegistry: LanguageRegistry,
reference: Grammar | string | null | undefined
): Grammar | undefined {
if (typeof reference === 'string') {
return languageRegistry.getLanguage(reference)?.grammar;
this: Prism,
reference: Grammar | string | null | undefined | Function
): Grammar | undefined | Function {
let prism = this ?? singleton;
let ret = reference ?? undefined;

if (typeof ret === 'string') {
ret = prism.languageRegistry.getLanguage(ret)?.grammar;
}

if (typeof ret === 'function' && ret.length === 0) {
// Function with no arguments, resolve eagerly
ret = ret.call(prism);
}

if (typeof ret === 'object' && ret.$rest) {
let restGrammar = resolve(ret.$rest, prism) ?? {};

Check failure on line 22 in src/core/tokenize/util.ts

View workflow job for this annotation

GitHub Actions / type-check

Expected 1 arguments, but got 2.

if (typeof restGrammar === 'object') {
ret = { ...ret, ...restGrammar };
}
}

return ret;

Check failure on line 29 in src/core/tokenize/util.ts

View workflow job for this annotation

GitHub Actions / type-check

Type 'string | Function | Grammar | undefined' is not assignable to type 'Function | Grammar | undefined'.
}

export function tokenizeByNamedGroups (match: RegExpExecArray): ({ type; content } | string)[] {
let str = match[0];
let result: ({ type; content } | string)[] = [];
let i = 0;

let entries = Object.entries(match.indices?.groups || {})
.map(([type, [start, end]]) => ({
type,
start: start - match.index,
end: end - match.index,
}))
.sort((a, b) => a.start - b.start);

for (let { type, start, end } of entries) {
if (start > i) {
result.push(str.slice(i, start));
}

let content = str.slice(start, end);
type = camelToKebabCase(type);
result.push({ type, content });
i = end;
}

if (i < str.length) {
result.push(str.slice(i));
}

return reference ?? undefined;
return result;
}
1 change: 1 addition & 0 deletions src/shared/languages/templating.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ function insertIntoHostToken (hostTokens: TokenStream, tokenStack: TokenStack) {

type GrammarRef = Grammar | string | undefined | null;

// TODO use resolve() from tokenize/util
function resolve (ref: GrammarRef, components: Registry): Grammar | undefined {
if (!ref) {
return undefined;
Expand Down
4 changes: 4 additions & 0 deletions src/shared/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,7 @@ export function kebabToCamelCase<T extends string> (kebab: T): KebabToCamelCase<
const [first, ...others] = kebab.split(/-/);
return (first + others.map(capitalize).join('')) as KebabToCamelCase<T>;
}

export function camelToKebabCase (str: string) {
return (str + '').replace(/[A-Z]/g, l => '-' + l.toLowerCase());
}
Loading