Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add syntax highlighting with Shikiji #17

Merged
merged 4 commits into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 5 additions & 0 deletions .changeset/unlucky-jokes-battle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"neogrok": minor
---

Add syntax highlighting with shikiji
1 change: 0 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@
node_modules
/build
/.svelte-kit
package-lock.json
svelte.config.js
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.yarn/releases/* -diff
10 changes: 5 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ jobs:
- uses: actions/setup-node@v3
with:
node-version: 20.x
- run: npm install
- run: npm run lint
- run: npm run test
- run: npm run build
- run: yarn install
- run: yarn run lint
- run: yarn run test
- run: yarn run build
deploy-demo:
# Only run on master commits; this is a further restriction of the top-level `on`.
if: github.event_name == 'push'
Expand Down Expand Up @@ -47,7 +47,7 @@ jobs:
- uses: actions/setup-node@v3
with:
node-version: 20.x
- run: npm install
- run: yarn install
- uses: changesets/action@v1
with:
commit: Publish release
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
*.log
node_modules/
*.tsbuildinfo
.npm
.svelte-kit
build/
.env
.envrc
.yarn/install-state.gz
1 change: 1 addition & 0 deletions .ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.yarn/releases
2 changes: 0 additions & 2 deletions .npmrc

This file was deleted.

1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v20.10.0
2 changes: 1 addition & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
node_modules
/build
/.svelte-kit
package-lock.json
/.yarn
49 changes: 49 additions & 0 deletions .yarn/patches/shikiji-core-npm-0.9.18-826293a3df.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
diff --git a/dist/chunk-types.d.mts b/dist/chunk-types.d.mts
index 2992357a1d0e670afce9ec11133a58a4e17593da..788cab9adb4134796ab6cd91fa9fd25b3a971dc5 100644
--- a/dist/chunk-types.d.mts
+++ b/dist/chunk-types.d.mts
@@ -1124,10 +1124,8 @@ interface ThemedTokenExplanation {
interface ThemedToken extends TokenStyles, TokenBase {
}
interface TokenBase {
- /**
- * The content of the token
- */
- content: string;
+ start: number;
+ end: number;
/**
* Explanation of
*
diff --git a/dist/index.mjs b/dist/index.mjs
index 6f891f5e37470cb673f8bcb8914c44634ca47091..1b52e8c87fa19d739e66f824715a18f37ae05fc6 100644
--- a/dist/index.mjs
+++ b/dist/index.mjs
@@ -3709,7 +3709,7 @@ function dimColor(color) {
function codeToThemedTokens(internal, code, options = {}) {
const { lang = 'text', theme: themeName = internal.getLoadedThemes()[0], } = options;
if (isPlaintext(lang)) {
- const lines = code.split(/\r\n|\r|\n/);
+ const lines = code;
return [...lines.map(line => [{ content: line }])];
}
const { theme, colorMap } = internal.setTheme(themeName);
@@ -3723,7 +3723,7 @@ function tokenizeWithTheme(code, grammar, theme, colorMap, options) {
...theme.colorReplacements,
...options?.colorReplacements,
};
- const lines = code.split(/\r\n|\r|\n/);
+ const lines = code;
let ruleStack = INITIAL;
let actual = [];
const final = [];
@@ -3754,7 +3754,8 @@ function tokenizeWithTheme(code, grammar, theme, colorMap, options) {
const foregroundColor = applyColorReplacements(colorMap[foreground], colorReplacements);
const fontStyle = StackElementMetadata.getFontStyle(metadata);
const token = {
- content: line.substring(startIndex, nextStartIndex),
+ start: startIndex,
+ end: nextStartIndex,
color: foregroundColor,
fontStyle,
};
13 changes: 13 additions & 0 deletions .yarn/patches/shikiji-npm-0.9.18-945ea5efcb.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
diff --git a/dist/bundle-full.d.mts b/dist/bundle-full.d.mts
index b8e655052e4a1b6e10e617eb06008a0782ff3a70..db1121f519c64bcada055f10f07500c27afca436 100644
--- a/dist/bundle-full.d.mts
+++ b/dist/bundle-full.d.mts
@@ -12,7 +12,7 @@ type Highlighter = HighlighterGeneric<BundledLanguage, BundledTheme>;
declare const getHighlighter: shikiji_core.GetHighlighterFactory<BundledLanguage, BundledTheme>;
declare const codeToHtml: (code: string, options: shikiji_core.CodeToHastOptions<BundledLanguage, BundledTheme>) => Promise<string>;
declare const codeToHast: (code: string, options: shikiji_core.CodeToHastOptions<BundledLanguage, BundledTheme>) => Promise<Root>;
-declare const codeToThemedTokens: (code: string, options: shikiji_core.RequireKeys<shikiji_core.CodeToThemedTokensOptions<BundledLanguage, BundledTheme>, "lang" | "theme">) => Promise<shikiji_core.ThemedToken[][]>;
+declare const codeToThemedTokens: (code: string[], options: shikiji_core.RequireKeys<shikiji_core.CodeToThemedTokensOptions<BundledLanguage, BundledTheme>, "lang" | "theme">) => Promise<shikiji_core.ThemedToken[][]>;
declare const codeToTokensWithThemes: (code: string, options: shikiji_core.RequireKeys<shikiji_core.CodeToTokensWithThemesOptions<BundledLanguage, BundledTheme>, "lang" | "themes">) => Promise<shikiji_core.ThemedTokenWithVariants[][]>;
declare const getSingletonHighlighter: () => Promise<HighlighterGeneric<BundledLanguage, BundledTheme>>;

893 changes: 893 additions & 0 deletions .yarn/releases/yarn-4.0.2.cjs

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
yarnPath: .yarn/releases/yarn-4.0.2.cjs
nodeLinker: node-modules
defaultSemverRangePrefix: ""
enableTelemetry: false
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ Neogrok is packaged for installation on
install an executable.

Alternatively, building from source is easy. Clone the repository,
`npm install && npm run build && npm run start`. You can of course run the server
without intermediation by `npm`, by doing whatever `npm run start` does directly;
but the relevant commands may change in the future, whereas `npm run start` will
`yarn install && yarn run build && yarn run start`. You can of course run the server
without intermediation by `yarn`, by doing whatever `yarn run start` does directly;
but the relevant commands may change in the future, whereas `yarn run start` will
not.

## Deploying
Expand Down
6 changes: 3 additions & 3 deletions demo/Dockerfile.neogrok
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
FROM node:20-slim as app-builder
WORKDIR /app
COPY . .
RUN npm install \
&& npm run build \
&& npm install --omit=dev
RUN yarn install \
&& yarn run build \
&& yarn workspaces focus --production

FROM gcr.io/distroless/nodejs20-debian12
WORKDIR /app
Expand Down
4 changes: 2 additions & 2 deletions demo/Dockerfile.neogrok.dockerignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
*
!.npmrc
!.yarn*
!yarn.lock
!.postcssrc
!package-lock.json
!package.json
!src/
!static/
Expand Down
Loading