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

feat(syntax-highlight): do client side #11654

Merged
merged 19 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
4cd53dd
wip(syntax-highlight): move prism to the client
fiji-flo Aug 19, 2024
1629d27
wip(syntax-highlight): types, no blocking, no allowlist, wrap in <code>
LeoMcA Aug 19, 2024
eae1973
wip(syntax-highlight): fix prism types in ai help
LeoMcA Aug 19, 2024
350bd45
wip(syntax-highlight): rename hook
LeoMcA Aug 19, 2024
d5c7eab
wip(syntax-highlight): clean up and rename build step
LeoMcA Aug 19, 2024
998111b
Merge remote-tracking branch 'origin/main' into client-syntax-highlight
LeoMcA Aug 22, 2024
2280c7e
Merge remote-tracking branch 'upstream/main' into client-syntax-highl…
fiji-flo Sep 5, 2024
5943f34
fix(syntax-highlight): handle aliases and dependencies
LeoMcA Sep 6, 2024
f1e01c0
Merge branch 'main' into client-syntax-highlight
LeoMcA Sep 6, 2024
8b77b12
fix(syntax-highlight): add react component for use in ai-help
LeoMcA Sep 6, 2024
713d8d8
wip(syntax-highlight): import client code async
LeoMcA Sep 9, 2024
b93c878
Merge remote-tracking branch 'origin/main' into client-syntax-highlight
LeoMcA Sep 10, 2024
aa1c5da
fix(syntax-highlight): don't return unescaped strings
LeoMcA Sep 10, 2024
325f3f5
Merge remote-tracking branch 'origin/main' into client-syntax-highlight
LeoMcA Sep 10, 2024
ddf0d0f
wip(syntax-highlight): review updates
LeoMcA Sep 10, 2024
34b2173
Merge branch 'main' into client-syntax-highlight
caugner Sep 11, 2024
d7ddd78
wip(syntax-highlighting): catch recursive errors and prism errors
LeoMcA Sep 16, 2024
7687cb8
Merge remote-tracking branch 'origin/main' into client-syntax-highlight
LeoMcA Sep 16, 2024
892d816
Merge remote-tracking branch 'origin/main' into client-syntax-highlight
LeoMcA Sep 16, 2024
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
4 changes: 2 additions & 2 deletions build/blog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
postProcessSmallerHeadingIDs,
} from "./utils.js";
import { slugToFolder } from "../libs/slug-utils/index.js";
import { syntaxHighlight } from "./syntax-highlight.js";
import { codeHeaders } from "./code-headers.js";
import { wrapTables } from "./wrap-tables.js";
import { Doc } from "../libs/types/document.js";
import { extractSections } from "./extract-sections.js";
Expand Down Expand Up @@ -391,7 +391,7 @@ export async function buildPost(
doc.hasMathML = true;
}
$("div.hidden").remove();
syntaxHighlight($, doc);
codeHeaders($);
injectNoTranslate($);
injectLoadingLazyAttributes($);
postProcessExternalLinks($);
Expand Down
45 changes: 45 additions & 0 deletions build/code-headers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import * as cheerio from "cheerio";

// Over the years we have accumulated some weird <pre> tags whose
// brush is more or less "junk".
// TODO: Perhaps, if you have a doc with <pre> tags that matches
// this, it should become a flaw.
const IGNORE = new Set(["none", "text", "plain", "unix"]);

/**
* Mutate the `$` instance by adding headers to <pre> tags containing code blocks.
*
*/
export function codeHeaders($: cheerio.CheerioAPI) {
LeoMcA marked this conversation as resolved.
Show resolved Hide resolved
// Our content will be like this: `<pre class="brush:js">` or
// `<pre class="brush: js">` so we're technically not looking for an exact
// match. The wildcard would technically match `<pre class="brushetta">`
// too. But within the loop, we do a more careful regex on the class name
// and only proceed if it's something sensible.
$("pre[class*=brush]").each((_, element) => {
// The language is whatever string comes after the `brush(:)`
// portion of the class name.
const $pre = $(element);

const className = $pre.attr("class").toLowerCase();
const match = className.match(/brush:?\s*([\w_-]+)/);
if (!match) {
return;
}
const name = match[1].replace("-nolint", "");
if (IGNORE.has(name)) {
// Seems to exist a couple of these in our docs. Just bail.
return;
}
const code = $pre.text();
$pre.wrapAll(`<div class='code-example'></div>`);
if (!$pre.hasClass("hidden")) {
$(
`<div class='example-header'><span class="language-name">${name}</span></div>`
).insertBefore($pre);
}
const $code = $("<code>").text(code);

$pre.empty().append($code);
});
}
4 changes: 2 additions & 2 deletions build/curriculum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { DocParent } from "../libs/types/document.js";
import { CURRICULUM_TITLE, DEFAULT_LOCALE } from "../libs/constants/index.js";
import * as kumascript from "../kumascript/index.js";
import LANGUAGES_RAW from "../libs/languages/index.js";
import { syntaxHighlight } from "./syntax-highlight.js";
import { codeHeaders } from "./code-headers.js";
import {
escapeRegExp,
injectLoadingLazyAttributes,
Expand Down Expand Up @@ -321,7 +321,7 @@ export async function buildCurriculumPage(
doc.hasMathML = true;
}
$("div.hidden").remove();
syntaxHighlight($, doc);
codeHeaders($);
injectNoTranslate($);
injectLoadingLazyAttributes($);
postProcessCurriculumLinks($, (p: string | undefined) => {
Expand Down
6 changes: 3 additions & 3 deletions build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
} from "./flaws/index.js";
import { checkImageReferences, checkImageWidths } from "./check-images.js";
import { getPageTitle } from "./page-title.js";
import { syntaxHighlight } from "./syntax-highlight.js";
import { codeHeaders } from "./code-headers.js";
import { formatNotecards } from "./format-notecards.js";
import buildOptions from "./build-options.js";
import LANGUAGES_RAW from "../libs/languages/index.js";
Expand Down Expand Up @@ -456,8 +456,8 @@ export async function buildDocument(
plainHTML = $.html();
}

// Apply syntax highlighting all <pre> tags.
syntaxHighlight($, doc);
// Add headers to all <pre> tags with code.
codeHeaders($);

// Post process HTML so that the right elements gets tagged so they
// *don't* get translated by tools like Google Translate.
Expand Down
139 changes: 0 additions & 139 deletions build/syntax-highlight.ts

This file was deleted.

7 changes: 2 additions & 5 deletions client/src/blog/post.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ import {
BlogPostLimitedMetadata,
AuthorMetadata,
} from "../../../libs/types/blog";
import {
useCopyExamplesToClipboardAndAIExplain,
useRunSample,
} from "../document/hooks";
import { useDecorateExamples, useRunSample } from "../document/hooks";
import { DEFAULT_LOCALE } from "../../../libs/constants";
import { SignUpSection as NewsletterSignUp } from "../newsletter";
import { TOC } from "../document/organisms/toc";
Expand Down Expand Up @@ -190,7 +187,7 @@ export function BlogPost(props: HydrationData) {
);
const { doc, blogMeta } = data || props || {};
useRunSample(doc);
useCopyExamplesToClipboardAndAIExplain(doc);
useDecorateExamples(doc);
return (
<>
{doc && blogMeta && (
Expand Down
Loading