denoland / deno-gfm

Server-side GitHub Flavored Markdown rendering for Deno
https://jsr.io/@deno/gfm
MIT License
221 stars 33 forks source link

syntax highlighting not working for "ts" ("typescript") #29

Closed andykais closed 1 year ago

andykais commented 1 year ago

https://deno.land/x/torm@1.0.2 (the code block tag is "ts")

Screen Shot 2023-01-06 at 10 05 33 AM

which renders properly on github https://github.com/andykais/

Screen Shot 2023-01-06 at 10 07 57 AM

torm

andykais commented 1 year ago

the following snippet seems to prove it:

(the code is tokenized with the "js" tag:

import { render } from "https://deno.land/x/gfm/mod.ts";

const markdown = `
\`\`\`js
console.log("Hello, world!");
\`\`\`
`;
const body = render(markdown,  "https://example.com");
console.log(body)
<div class="highlight notranslate"><pre>console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"Hello, world!"</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></div>

and with the "ts" tag it is not:

import { render } from "https://deno.land/x/gfm/mod.ts";

const markdown = `
\`\`\`ts
console.log("Hello, world!");
\`\`\`
`;
const body = render(markdown,  "https://example.com");
console.log(body)
<pre><code>console.log("Hello, world!");</code></pre>

I have tried "ts", "typescript" and "Typescript", all of which seem to have no tokenization

andykais commented 1 year ago

I think I found the solution, in deps.ts you need to load typescript as a language (probably a more robust solution would be to dynamically load the languages depending on what is demanded of the rendered). That, or come up with a hard list of supported code tags. Im not sure how exactly this renderer is ran in production though, so thats probably a better question for you guys to answer.

// deps.ts
import Prism from "npm:prismjs@1.29.0"
import loadLanguages from 'npm:prismjs@1.29.0/components/index.js'
loadLanguages(['typescript']);

export { Prism }

unfortunately my job makes it difficult to contribute back to existing open source libraries, otherwise I would open a PR 😞

lino-levan commented 1 year ago

In the README it states:

By default syntax highlighting for JavaScript, Markdown, and HTML is included. You can include more languages importing them.

Typescript is not on that list (even if I personally think it should be).

Unless you are suggesting to auto-load languages depending on the code blocks that deno-gfm encounters, this isn't an actionable issue.

lino-levan commented 1 year ago

After reading https://github.com/denoland/dotland/issues/2653, this issue becomes more clear. To summarize: sometimes language support doesn't work even if that language support is explicitly loaded.

see: https://deno.land/x/torm@1.0.2 for an example.

tmchein commented 1 year ago

Do you still have this issue? I checked your library and it seems to be highlighted.

lino-levan commented 1 year ago

I think https://github.com/denoland/dotland/pull/2659 fixed the downstream issue. I don't think there was an issue with upstream (this repo).