-
Hi! I use Framework as a computational journalist. It's very useful for prototyping projects and explaining how the data has been manipulated. I use the fenced code blocks with Is there a way to grab the content of a file and put it inside a fenced code block that would not run? Thank you. 🙏 |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
One way to do this is with a page loader. This allows you to generate the Markdown dynamically (for example using Node.js) and you can read the JavaScript/TypeScript file and then inline it into the Markdown rather than copying it by hand. Alternatively you could load the source as a |
Beta Was this translation helpful? Give feedback.
-
Thanks @mbostock! Your suggestion helped me write this function. import { FileAttachment } from "npm:@observablehq/stdlib";
import { html } from "htl";
import hljs from "highlight.js/lib/core";
import typescript from "highlight.js/lib/languages/typescript";
hljs.registerLanguage("typescript", typescript);
export default async function getHighlightedCode(file: FileAttachment) {
const code = await file.text();
const highlightedCode = hljs.highlight(code, {
language: "typescript",
}).value;
const wrappedCode = html`<div
class="observablehq-pre-container"
data-language="ts"
>
<pre data-language="ts"><code class="language-ts"></code></pre>
</div>`;
wrappedCode.querySelector("code").innerHTML = highlightedCode;
return wrappedCode;
} Which can be called this way in the markdown.
And it works! :) |
Beta Was this translation helpful? Give feedback.
Thanks @mbostock!
Your suggestion helped me write this function.