feature: vor und nachteile

This commit is contained in:
Hikyu
2026-05-31 13:54:31 +02:00
parent 552a947a1c
commit e3dad17446
23270 changed files with 3024794 additions and 200 deletions
+22
View File
@@ -0,0 +1,22 @@
MIT License
Copyright (c) 2021 Pine Wu
Copyright (c) 2023 Anthony Fu <https://github.com/antfu>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
+9
View File
@@ -0,0 +1,9 @@
# @shikijs/monaco
Use [shiki](https://github.com/shikijs/shiki) to highlight [Monaco Editor](https://microsoft.github.io/monaco-editor/).
[Documentation](https://shiki.style/packages/monaco)
## License
MIT
+23
View File
@@ -0,0 +1,23 @@
import { ThemeRegistrationResolved, ShikiInternal } from '@shikijs/types';
import monacoNs from 'monaco-editor-core';
interface MonacoTheme extends monacoNs.editor.IStandaloneThemeData {
}
interface ShikiToMonacoOptions {
/**
* The maximum length of a line to tokenize.
*
* @default 20000
*/
tokenizeMaxLineLength?: number;
/**
* The time limit in milliseconds for tokenizing a line.
*
* @default 500
*/
tokenizeTimeLimit?: number;
}
declare function textmateThemeToMonacoTheme(theme: ThemeRegistrationResolved): MonacoTheme;
declare function shikiToMonaco(highlighter: ShikiInternal<any, any>, monaco: typeof monacoNs, options?: ShikiToMonacoOptions): void;
export { type MonacoTheme, type ShikiToMonacoOptions, shikiToMonaco, textmateThemeToMonacoTheme };
+23
View File
@@ -0,0 +1,23 @@
import { ThemeRegistrationResolved, ShikiInternal } from '@shikijs/types';
import monacoNs from 'monaco-editor-core';
interface MonacoTheme extends monacoNs.editor.IStandaloneThemeData {
}
interface ShikiToMonacoOptions {
/**
* The maximum length of a line to tokenize.
*
* @default 20000
*/
tokenizeMaxLineLength?: number;
/**
* The time limit in milliseconds for tokenizing a line.
*
* @default 500
*/
tokenizeTimeLimit?: number;
}
declare function textmateThemeToMonacoTheme(theme: ThemeRegistrationResolved): MonacoTheme;
declare function shikiToMonaco(highlighter: ShikiInternal<any, any>, monaco: typeof monacoNs, options?: ShikiToMonacoOptions): void;
export { type MonacoTheme, type ShikiToMonacoOptions, shikiToMonaco, textmateThemeToMonacoTheme };
+126
View File
@@ -0,0 +1,126 @@
import { INITIAL, EncodedTokenMetadata } from '@shikijs/vscode-textmate';
function textmateThemeToMonacoTheme(theme) {
let rules = "rules" in theme ? theme.rules : undefined;
if (!rules) {
rules = [];
const themeSettings = theme.settings || theme.tokenColors;
for (const { scope, settings: { foreground, background, fontStyle } } of themeSettings) {
const scopes = Array.isArray(scope) ? scope : [scope];
for (const s of scopes) {
if (s && (foreground || background || fontStyle)) {
rules.push({
token: s,
foreground: normalizeColor(foreground),
background: normalizeColor(background),
fontStyle
});
}
}
}
}
const colors = Object.fromEntries(
Object.entries(theme.colors || {}).map(([key, value]) => [key, `#${normalizeColor(value)}`])
);
return {
base: theme.type === "light" ? "vs" : "vs-dark",
inherit: false,
colors,
rules
};
}
function shikiToMonaco(highlighter, monaco, options = {}) {
const themeMap = /* @__PURE__ */ new Map();
const themeIds = highlighter.getLoadedThemes();
for (const themeId of themeIds) {
const tmTheme = highlighter.getTheme(themeId);
const monacoTheme = textmateThemeToMonacoTheme(tmTheme);
themeMap.set(themeId, monacoTheme);
monaco.editor.defineTheme(themeId, monacoTheme);
}
const colorMap = [];
const colorToScopeMap = /* @__PURE__ */ new Map();
const _setTheme = monaco.editor.setTheme.bind(monaco.editor);
monaco.editor.setTheme = (themeName) => {
const ret = highlighter.setTheme(themeName);
const theme = themeMap.get(themeName);
colorMap.length = ret.colorMap.length;
ret.colorMap.forEach((color, i) => {
colorMap[i] = color;
});
colorToScopeMap.clear();
theme?.rules.forEach((rule) => {
const c = normalizeColor(rule.foreground);
if (c && !colorToScopeMap.has(c))
colorToScopeMap.set(c, rule.token);
});
_setTheme(themeName);
};
monaco.editor.setTheme(themeIds[0]);
function findScopeByColor(color) {
return colorToScopeMap.get(color);
}
const {
tokenizeMaxLineLength = 2e4,
tokenizeTimeLimit = 500
} = options;
const monacoLanguageIds = new Set(monaco.languages.getLanguages().map((l) => l.id));
for (const lang of highlighter.getLoadedLanguages()) {
if (monacoLanguageIds.has(lang)) {
monaco.languages.setTokensProvider(lang, {
getInitialState() {
return new TokenizerState(INITIAL);
},
tokenize(line, state) {
if (line.length >= tokenizeMaxLineLength) {
return {
endState: state,
tokens: [{ startIndex: 0, scopes: "" }]
};
}
const grammar = highlighter.getLanguage(lang);
const result = grammar.tokenizeLine2(line, state.ruleStack, tokenizeTimeLimit);
if (result.stoppedEarly)
console.warn(`Time limit reached when tokenizing line: ${line.substring(0, 100)}`);
const tokensLength = result.tokens.length / 2;
const tokens = [];
for (let j = 0; j < tokensLength; j++) {
const startIndex = result.tokens[2 * j];
const metadata = result.tokens[2 * j + 1];
const color = normalizeColor(colorMap[EncodedTokenMetadata.getForeground(metadata)] || "");
const scope = findScopeByColor(color) || "";
tokens.push({ startIndex, scopes: scope });
}
return { endState: new TokenizerState(result.ruleStack), tokens };
}
});
}
}
}
class TokenizerState {
constructor(_ruleStack) {
this._ruleStack = _ruleStack;
}
get ruleStack() {
return this._ruleStack;
}
clone() {
return new TokenizerState(this._ruleStack);
}
equals(other) {
if (!other || !(other instanceof TokenizerState) || other !== this || other._ruleStack !== this._ruleStack) {
return false;
}
return true;
}
}
function normalizeColor(color) {
if (!color)
return color;
color = (color.charCodeAt(0) === 35 ? color.slice(1) : color).toLowerCase();
if (color.length === 3 || color.length === 4)
color = color.split("").map((c) => c + c).join("");
return color;
}
export { shikiToMonaco, textmateThemeToMonacoTheme };
+55
View File
@@ -0,0 +1,55 @@
{
"name": "@shikijs/monaco",
"type": "module",
"version": "1.29.2",
"description": "Use Shiki for Monaco Editor",
"author": "Anthony Fu <anthonyfu117@hotmail.com>",
"license": "MIT",
"homepage": "https://github.com/shikijs/shiki#readme",
"repository": {
"type": "git",
"url": "git+https://github.com/shikijs/shiki.git",
"directory": "packages/monaco"
},
"bugs": "https://github.com/shikijs/shiki/issues",
"keywords": [
"shiki",
"monaco-editor"
],
"sideEffects": false,
"exports": {
".": {
"types": "./dist/index.d.mts",
"default": "./dist/index.mjs"
},
"./*": "./dist/*"
},
"main": "./dist/index.mjs",
"module": "./dist/index.mjs",
"types": "./dist/index.d.mts",
"typesVersions": {
"*": {
"*": [
"./dist/*",
"./*"
]
}
},
"files": [
"dist"
],
"dependencies": {
"@shikijs/vscode-textmate": "^10.0.1",
"@shikijs/types": "1.29.2",
"@shikijs/core": "1.29.2"
},
"devDependencies": {
"monaco-editor-core": "^0.52.2"
},
"scripts": {
"build": "unbuild",
"dev": "unbuild --stub",
"test": "vitest",
"play": "nr -C playground play"
}
}