mirror of
https://seed.radicle.dev/rad:z4V1sjrXqjvFdnCUbxPFqd5p4DtH5.git
synced 2026-11-21 06:10:44 +08:00
Move syntax highlighting to web client
Signed-off-by: Sebastian Martinez <me@sebastinez.dev>
This commit is contained in:
+29
-2
@@ -4,6 +4,7 @@
|
||||
import dompurify from "dompurify";
|
||||
import matter from "@radicle/gray-matter";
|
||||
import { base } from "@app/router";
|
||||
import { highlight } from "@app/syntax";
|
||||
import {
|
||||
markdownExtensions as extensions,
|
||||
renderer,
|
||||
@@ -14,6 +15,7 @@
|
||||
} from "@app/utils";
|
||||
import { marked } from "marked";
|
||||
import { onMount } from "svelte";
|
||||
import { toDom } from "hast-util-to-dom";
|
||||
|
||||
export let content: string;
|
||||
export let doc = matter(content);
|
||||
@@ -29,7 +31,7 @@
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention
|
||||
dompurify.sanitize(marked.parse(content), { SANITIZE_DOM: false });
|
||||
|
||||
onMount(() => {
|
||||
onMount(async () => {
|
||||
// Don't underline <a> tags that contain images.
|
||||
const elems = container.querySelectorAll("a");
|
||||
|
||||
@@ -59,6 +61,31 @@
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Replaces code blocks in the background with highlighted code.
|
||||
const prefix = "language-";
|
||||
const nodes = Array.from(document.body.querySelectorAll("pre code"));
|
||||
|
||||
const treeChanges: Promise<void>[] = [];
|
||||
|
||||
for (const node of nodes) {
|
||||
const className = Array.from(node.classList).find(name =>
|
||||
name.startsWith(prefix),
|
||||
);
|
||||
if (!className) continue;
|
||||
|
||||
treeChanges.push(
|
||||
highlight(node.textContent ?? "", className.slice(prefix.length))
|
||||
.then(tree => {
|
||||
if (tree) {
|
||||
node.replaceChildren(toDom(tree, { fragment: true }));
|
||||
}
|
||||
})
|
||||
.catch(e => console.warn("Not able to highlight code block", e)),
|
||||
);
|
||||
}
|
||||
|
||||
await Promise.allSettled(treeChanges);
|
||||
});
|
||||
</script>
|
||||
|
||||
@@ -160,7 +187,7 @@
|
||||
.markdown :global(code) {
|
||||
font-family: var(--font-family-monospace);
|
||||
font-size: var(--font-size-regular);
|
||||
color: var(--color-secondary-6);
|
||||
color: var(--color-foreground);
|
||||
background-color: var(--color-foreground-2);
|
||||
border-radius: 0.5rem;
|
||||
padding: 0.125rem 0.25rem;
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
<script lang="ts">
|
||||
import type { Blob } from "@app/project";
|
||||
import type { MaybeHighlighted } from "@app/syntax";
|
||||
import type { ProjectRoute } from "@app/router/definitions";
|
||||
|
||||
import HeaderToggleLabel from "@app/base/projects/HeaderToggleLabel.svelte";
|
||||
import ProjectLink from "@app/router/ProjectLink.svelte";
|
||||
import Readme from "@app/base/projects/Readme.svelte";
|
||||
import { afterUpdate, beforeUpdate, onMount } from "svelte";
|
||||
import { highlight } from "@app/syntax";
|
||||
import { isMarkdownPath, scrollIntoView, twemoji } from "@app/utils";
|
||||
import { onMount } from "svelte";
|
||||
import { lineNumbersGutter } from "@app/syntax";
|
||||
import { toHtml } from "hast-util-to-html";
|
||||
import { updateProjectRoute } from "@app/router";
|
||||
|
||||
export let activeRoute: ProjectRoute;
|
||||
@@ -14,25 +17,42 @@
|
||||
export let getImage: (path: string) => Promise<Blob>;
|
||||
export let line: string | undefined = undefined;
|
||||
|
||||
$: lineNumber = line ? parseInt(line.substring(1)) : undefined;
|
||||
|
||||
const fileExtension = blob.path.split(".").pop() ?? "";
|
||||
const lastCommit = blob.info.lastCommit;
|
||||
const lines = blob.binary ? 0 : (blob.content.match(/\n/g) || []).length;
|
||||
const hasFinalNewline = blob.content.endsWith("\n");
|
||||
const lineNumbers = Array(hasFinalNewline ? lines : lines + 1)
|
||||
.fill(0)
|
||||
.map((_, index) => (index + 1).toString());
|
||||
const parentDir = blob.path
|
||||
.match(/^.*\/|/)
|
||||
?.values()
|
||||
.next().value;
|
||||
let content: MaybeHighlighted = undefined;
|
||||
|
||||
// Waiting onMount, due to the line numbers still loading.
|
||||
onMount(() => {
|
||||
if (line) {
|
||||
scrollIntoView(line);
|
||||
// Any time a user clicks on a line number, the `line` prop gets updated,
|
||||
// and the line is highlighted, but the previous line is not unhighlighted.
|
||||
// So we have to make sure here that any previous highlighting gets removed,
|
||||
// before updating the component.
|
||||
beforeUpdate(() => {
|
||||
for (const item of document.getElementsByClassName("highlight")) {
|
||||
item.classList.remove("highlight");
|
||||
}
|
||||
});
|
||||
|
||||
onMount(async () => {
|
||||
const output = await highlight(blob.content, fileExtension);
|
||||
if (output) {
|
||||
content = lineNumbersGutter(output);
|
||||
}
|
||||
});
|
||||
|
||||
afterUpdate(() => {
|
||||
if (line) {
|
||||
scrollIntoView(line);
|
||||
|
||||
const element = document.getElementById(line);
|
||||
if (element) {
|
||||
element.classList.add("highlight");
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
const isMarkdown = isMarkdownPath(blob.path);
|
||||
// If we have a line number we should show the raw output.
|
||||
let showMarkdown = line ? false : isMarkdown;
|
||||
@@ -40,10 +60,6 @@
|
||||
updateProjectRoute({ line: undefined });
|
||||
showMarkdown = !showMarkdown;
|
||||
};
|
||||
|
||||
$: if (line) {
|
||||
scrollIntoView(line);
|
||||
}
|
||||
</script>
|
||||
|
||||
<style>
|
||||
@@ -100,29 +116,51 @@
|
||||
margin-right: 0.5rem;
|
||||
}
|
||||
|
||||
.line-numbers {
|
||||
.code :global(.line-number) {
|
||||
color: var(--color-foreground-4);
|
||||
font-family: var(--font-family-sans-serif);
|
||||
text-align: right;
|
||||
user-select: none;
|
||||
padding: 1rem 1rem 0.5rem 1rem;
|
||||
padding-right: 1rem;
|
||||
padding-left: 1rem;
|
||||
}
|
||||
.line-number {
|
||||
display: block;
|
||||
.code :global(.line-number:hover) {
|
||||
cursor: pointer;
|
||||
color: var(--color-foreground);
|
||||
}
|
||||
.line-number:hover,
|
||||
.line-number.highlighted {
|
||||
color: var(--color-foreground-6);
|
||||
|
||||
.code :global(.content) {
|
||||
display: inline;
|
||||
font-family: var(--font-family-monospace);
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
.code :global(.line) {
|
||||
line-height: 22px; /* This seems to be the line-height of a pre code block */
|
||||
}
|
||||
.code :global(.highlight) {
|
||||
background-color: var(--color-caution-3);
|
||||
}
|
||||
.code :global(.highlight td a) {
|
||||
color: var(--color-foreground);
|
||||
}
|
||||
|
||||
.code :global(.line-content) {
|
||||
padding: 0;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.code {
|
||||
padding-bottom: 0.5rem;
|
||||
width: 100%;
|
||||
border-spacing: 0;
|
||||
overflow-x: auto;
|
||||
font-size: 1rem;
|
||||
padding-top: 1rem;
|
||||
margin-bottom: 1.5rem;
|
||||
}
|
||||
|
||||
.container {
|
||||
position: relative;
|
||||
display: flex;
|
||||
overflow-x: auto;
|
||||
border: 1px solid var(--color-foreground-3);
|
||||
border-top-style: dashed;
|
||||
border-bottom-left-radius: var(--border-radius-small);
|
||||
@@ -144,14 +182,6 @@
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.highlight {
|
||||
position: absolute;
|
||||
width: 100%;
|
||||
height: 1.5rem;
|
||||
top: 1rem;
|
||||
background-color: var(--color-caution-3);
|
||||
}
|
||||
|
||||
.no-scrollbar {
|
||||
scrollbar-width: none;
|
||||
}
|
||||
@@ -165,13 +195,9 @@
|
||||
}
|
||||
|
||||
@media (max-width: 960px) {
|
||||
.code,
|
||||
.line-numbers {
|
||||
.code {
|
||||
font-size: var(--font-size-small);
|
||||
}
|
||||
.highlight {
|
||||
display: none;
|
||||
}
|
||||
}
|
||||
|
||||
@media (max-width: 720px) {
|
||||
@@ -215,27 +241,10 @@
|
||||
</div>
|
||||
{:else if showMarkdown}
|
||||
<Readme content={blob.content} {getImage} {activeRoute} />
|
||||
{:else}
|
||||
{#if lineNumber}
|
||||
<div
|
||||
class="highlight"
|
||||
style="top: {lineNumber === 1 ? 1 : 1.5 * lineNumber - 0.5}rem" />
|
||||
{/if}
|
||||
<div class="line-numbers">
|
||||
{#each lineNumbers as lineNumber}
|
||||
<ProjectLink
|
||||
projectParams={{ line: `L${lineNumber}` }}
|
||||
id="L{lineNumber}">
|
||||
<span class="line-number" class:highlighted={lineNumber === line} />
|
||||
{lineNumber}
|
||||
</ProjectLink>
|
||||
{/each}
|
||||
</div>
|
||||
{#if blob.html}
|
||||
<pre class="code no-scrollbar">{@html blob.content}</pre>
|
||||
{:else}
|
||||
<pre class="code txt-monospace no-scrollbar">{blob.content}</pre>
|
||||
{/if}
|
||||
{:else if content}
|
||||
<table class="code no-scrollbar">
|
||||
{@html toHtml(content)}
|
||||
</table>
|
||||
{/if}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -47,17 +47,8 @@
|
||||
return state.blob;
|
||||
}
|
||||
|
||||
const isMarkdownPath = utils.isMarkdownPath(path);
|
||||
const promise =
|
||||
path === "/"
|
||||
? project.getReadme(commit)
|
||||
: project.getBlob(
|
||||
commit,
|
||||
path,
|
||||
isMarkdownPath
|
||||
? { highlight: false }
|
||||
: { highlight: true, theme: `base16-ocean.${theme}` },
|
||||
);
|
||||
path === "/" ? project.getReadme(commit) : project.getBlob(commit, path);
|
||||
|
||||
state = { status: Status.Loading, path };
|
||||
try {
|
||||
@@ -71,7 +62,7 @@
|
||||
// Get an image blob based on a relative path.
|
||||
const getImage = async (imagePath: string): Promise<proj.Blob> => {
|
||||
const finalPath = utils.canonicalize(imagePath, path);
|
||||
return project.getBlob(commit, finalPath, { highlight: false });
|
||||
return project.getBlob(commit, finalPath);
|
||||
};
|
||||
|
||||
const onSelect = async (newPath: string, theme: Theme) => {
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
const getImage = async (imagePath: string): Promise<Blob> => {
|
||||
const finalPath = canonicalize(imagePath, "/"); // We only use the root path in issues.
|
||||
const commit = project.branches[project.defaultBranch]; // We suppose that all issues are only looked at on HEAD of the default branch.
|
||||
return project.getBlob(commit, finalPath, { highlight: false });
|
||||
return project.getBlob(commit, finalPath);
|
||||
};
|
||||
</script>
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
const getImage = async (imagePath: string): Promise<Blob> => {
|
||||
const finalPath = canonicalize(imagePath, "/"); // We only use the root path in issues.
|
||||
const commit = project.branches[project.defaultBranch]; // We suppose that all issues are only looked at on HEAD of the default branch.
|
||||
return project.getBlob(commit, finalPath, { highlight: false });
|
||||
return project.getBlob(commit, finalPath);
|
||||
};
|
||||
</script>
|
||||
|
||||
|
||||
+2
-8
@@ -278,17 +278,11 @@ export class Project implements ProjectInfo {
|
||||
).get();
|
||||
}
|
||||
|
||||
async getBlob(
|
||||
commit: string,
|
||||
path: string,
|
||||
options:
|
||||
| { highlight: false }
|
||||
| { highlight: true; theme: "base16-ocean.dark" | "base16-ocean.light" },
|
||||
): Promise<Blob> {
|
||||
async getBlob(commit: string, path: string): Promise<Blob> {
|
||||
return new Request(
|
||||
`projects/${this.urn}/blob/${commit}/${path}`,
|
||||
this.seed.api,
|
||||
).get(options);
|
||||
).get();
|
||||
}
|
||||
|
||||
async getReadme(commit: string): Promise<Blob> {
|
||||
|
||||
+5
-1
@@ -23,7 +23,11 @@ export const base = window.HASH_ROUTING ? "./" : "/";
|
||||
window.addEventListener("hashchange", e => {
|
||||
const route = pathToRoute(e.newURL);
|
||||
if (route?.resource === "projects" && route.params.hash) {
|
||||
updateProjectRoute({ hash: route.params.hash });
|
||||
if (route.params.hash.match(/^L\d+$/)) {
|
||||
updateProjectRoute({ line: route.params.hash });
|
||||
} else {
|
||||
updateProjectRoute({ hash: route.params.hash });
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -0,0 +1,159 @@
|
||||
import type { Root } from "@wooorm/starry-night";
|
||||
import type { ElementContent } from "hast";
|
||||
|
||||
import { createStarryNight, common } from "@wooorm/starry-night";
|
||||
import sourceTsx from "@wooorm/starry-night/lang/source.tsx";
|
||||
import sourceSvelte from "@wooorm/starry-night/lang/source.svelte.js";
|
||||
import sourceSolidity from "@wooorm/starry-night/lang/source.solidity.js";
|
||||
import sourceToml from "@wooorm/starry-night/lang/source.toml";
|
||||
import sourceErlang from "@wooorm/starry-night/lang/source.erlang.js";
|
||||
import sourceDockerfile from "@wooorm/starry-night/lang/source.dockerfile";
|
||||
import sourceAsciiDoc from "@wooorm/starry-night/lang/text.html.asciidoc";
|
||||
|
||||
export type MaybeHighlighted = Root | undefined;
|
||||
|
||||
export const grammars = [
|
||||
...common,
|
||||
sourceSvelte,
|
||||
sourceSolidity,
|
||||
sourceTsx,
|
||||
sourceErlang,
|
||||
sourceDockerfile,
|
||||
sourceAsciiDoc,
|
||||
sourceToml,
|
||||
// A grammar that doesn't do any parsing, but needed for files without a known filetype.
|
||||
{
|
||||
extensions: [""],
|
||||
names: ["raw-format"],
|
||||
patterns: [],
|
||||
scopeName: "text.raw",
|
||||
},
|
||||
];
|
||||
|
||||
let starryNight: Awaited<ReturnType<typeof createStarryNight>>;
|
||||
|
||||
export async function highlight(
|
||||
content: string,
|
||||
grammar: string,
|
||||
): Promise<MaybeHighlighted> {
|
||||
if (starryNight === undefined) {
|
||||
starryNight = await createStarryNight(grammars);
|
||||
}
|
||||
const scope = starryNight.flagToScope(grammar);
|
||||
return starryNight.highlight(content, scope ?? "text.raw");
|
||||
}
|
||||
|
||||
export function lineNumbersGutter(tree: Root) {
|
||||
const replacement: ElementContent[] = [];
|
||||
const search = /\r?\n|\r/g;
|
||||
let index = -1;
|
||||
let start = 0;
|
||||
let startTextRemainder = "";
|
||||
let lineNumber = 0;
|
||||
|
||||
while (++index < tree.children.length) {
|
||||
const child = tree.children[index];
|
||||
|
||||
if (child.type === "text") {
|
||||
let textStart = 0;
|
||||
let match = search.exec(child.value);
|
||||
|
||||
while (match) {
|
||||
// Nodes in this line.
|
||||
const line = tree.children.slice(start, index) as ElementContent[];
|
||||
|
||||
// Prepend text from a partial matched earlier text.
|
||||
if (startTextRemainder) {
|
||||
line.unshift({ type: "text", value: startTextRemainder });
|
||||
startTextRemainder = "";
|
||||
}
|
||||
|
||||
// Append text from this text.
|
||||
if (match.index > textStart) {
|
||||
line.push({
|
||||
type: "text",
|
||||
value: child.value.slice(textStart, match.index),
|
||||
});
|
||||
}
|
||||
|
||||
// Add a line, and the eol.
|
||||
lineNumber += 1;
|
||||
replacement.push(createLine(line, lineNumber), {
|
||||
type: "text",
|
||||
value: match[0],
|
||||
});
|
||||
|
||||
start = index + 1;
|
||||
textStart = match.index + match[0].length;
|
||||
match = search.exec(child.value);
|
||||
}
|
||||
|
||||
// If we matched, make sure to not drop the text after the last line ending.
|
||||
if (start === index + 1) {
|
||||
startTextRemainder = child.value.slice(textStart);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const line = tree.children.slice(start) as ElementContent[];
|
||||
// Prepend text from a partial matched earlier text.
|
||||
if (startTextRemainder) {
|
||||
line.unshift({ type: "text", value: startTextRemainder });
|
||||
startTextRemainder = "";
|
||||
}
|
||||
|
||||
if (line.length > 0) {
|
||||
lineNumber += 1;
|
||||
replacement.push(createLine(line, lineNumber));
|
||||
}
|
||||
|
||||
// Replace children with new array.
|
||||
tree.children = replacement;
|
||||
|
||||
return tree;
|
||||
}
|
||||
|
||||
function createLine(children: ElementContent[], line: number): ElementContent {
|
||||
return {
|
||||
type: "element",
|
||||
tagName: "tr",
|
||||
properties: {
|
||||
class: "line",
|
||||
id: "L" + line,
|
||||
},
|
||||
children: [
|
||||
{
|
||||
type: "element",
|
||||
tagName: "td",
|
||||
properties: {
|
||||
className: "line-number",
|
||||
},
|
||||
children: [
|
||||
{
|
||||
type: "element",
|
||||
tagName: "a",
|
||||
properties: { href: "#L" + line },
|
||||
children: [{ type: "text", value: line.toString() }],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
type: "element",
|
||||
tagName: "td",
|
||||
properties: {
|
||||
className: "line-content",
|
||||
},
|
||||
children: [
|
||||
{
|
||||
type: "element",
|
||||
tagName: "pre",
|
||||
properties: {
|
||||
className: "content",
|
||||
},
|
||||
children,
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user