Skip to content

Displaying the content of a file inside a fenced code block #1742

Answered by nshiab
nshiab asked this question in Q&A
Discussion options

You must be logged in to vote

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.

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by Fil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants