Skip to content
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
22 changes: 22 additions & 0 deletions components/Book/Book.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { SidenoteContext } from "@/components/Book/Sidenote";
import { useHasMounted } from "@/hooks/useHasMounted";
import { usePublicProvider } from "@/hooks/usePublicProvider";
import { ChapterDef, LinkDesc, UnlockChaptersOnAnswersType } from "@/types";
import { initCopyButtons } from "@/utils/copyToClipboard";


const Chapters = ({chapters: allChapters, bookId, chapterNumbers, unlockChaptersOnAnswers, env, setIsChapterIndexVisible, allAnswers}:
Expand Down Expand Up @@ -241,6 +242,27 @@ export const Book = (
}
}, [layout]);

// Expressive code insert scripts into mdx, which are not executed,
// so we imitate what they would do.
React.useEffect(() => {
const run = () => {
initCopyButtons(document);

document.querySelectorAll('.expressive-code pre').forEach((el) => {
// reading these forces layout flush
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
el.scrollWidth;
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
el.clientWidth;
})
window.dispatchEvent(new Event('resize'));
}
run();
const obs = new MutationObserver(() => { run(); })
obs.observe(document.body, { childList: true, subtree: true });
return () => obs.disconnect();
}, []);
Comment thread
janezd marked this conversation as resolved.

// If there are no chapters, ensure enough space for any side notes in intro.
// Doing this in presence of chapters would increase the left margin too much;
// let us assume that books with chapters don't have side notes in intro.
Expand Down
12 changes: 10 additions & 2 deletions ingest/md-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ import rehypeKatex from "rehype-katex";
import rehypeExpressiveCode from "rehype-expressive-code";
import { pluginCollapsibleSections } from "@expressive-code/plugin-collapsible-sections";

import { addRelativeDir, forbiddenComponents, rewriteQuestions, replacer, constructReplacer } from "./plugins";
import {
addRelativeDir,
forbiddenComponents,
rewriteQuestions,
replacer,
constructReplacer,
removeTerminalFrames
} from "./plugins";
import { getImageSize } from "./getImageSize";
import { isDirectory, joinedPath, readPublicDir } from "./paths";
import {ChapterFrontmatter, CollectionFrontmatter, RawBookFrontmatter} from "@/types";
Expand Down Expand Up @@ -187,7 +194,8 @@ export const serializedContent = async (
addRelativeDir( { relativeDir }),
getImageSize,
rewriteQuestions,
[rehypeExpressiveCode, rehypeExpressiveCodeOptions]
[rehypeExpressiveCode, rehypeExpressiveCodeOptions],
removeTerminalFrames
],
});
return compiled.value as string;
Expand Down
23 changes: 23 additions & 0 deletions ingest/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,26 @@ export const rewriteQuestions = () => (tree: Root) => {
}
});
};

/* Life's too short to try to convince expressive code
to stop adding frames to terminal windows, so let's just remove them. */
export const removeTerminalFrames = () => {
return (tree: Root) => {
visit(tree, 'element', (node: any) => {
if (!node.properties?.className) {
return;
}
const className = node.properties.className
if (Array.isArray(className)) {
node.properties.className = className.filter(
(c) => c !== 'is-terminal'
);
} else if (typeof className === 'string') {
node.properties.className = className
.split(' ')
.filter((c) => c !== 'is-terminal')
.join(' ')
}
})
}
}
1 change: 1 addition & 0 deletions styles/content-index/content-index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
position: absolute;
top: 24px;
display: none;
max-width: 500px;
}

.header-content-index:hover .content-index-at-header {
Expand Down
47 changes: 47 additions & 0 deletions utils/copyToClipboard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const fallbackCopy = (text: string): boolean => {
const el = document.createElement('textarea');
el.value = text;

// prevent scroll jump
el.style.position = 'fixed';
el.style.top = '0';
el.style.left = '0';
el.style.opacity = '0';
el.style.pointerEvents = 'none';

document.body.appendChild(el);
el.focus();
el.select();

let success = false;
try {
success = document.execCommand('copy');
} catch {
success = false;
}

document.body.removeChild(el);
return success;
}

export const initCopyButtons = (root = document) => {
root.querySelectorAll('.expressive-code .copy button').forEach((btn) => {
if ((btn as any)._wired) {
return;
}
(btn as any)._wired = true

btn.addEventListener('click', async () => {
const code = btn.getAttribute('data-code')?.replace(/\u007f/g, '\n');
if (!code) {
return;
}

try {
await navigator.clipboard.writeText(code);
} catch {
fallbackCopy(code);
}
})
})
}
Comment thread
janezd marked this conversation as resolved.
Loading