Open Benjaminsson opened 3 years ago
:+1: Am getting the exact same issue
import remarkTorchlight from "remark-torchlight";
error - /home/~~Redacted~~/node_modules/remark-torchlight/index.js:1
import parse5 from 'parse5'
^^^^^^
SyntaxError: Cannot use import statement outside a module
Same issue here
Also preventing me using in NextJS (swc
compiler) 🙁 @aarondfrancis is there something we can help with toward getting this resolved?
Ok yall, thanks for your patience. I just updated the underlying library and this library to ESM, but now the test is failing. It worked with the old node_modules versions I had installed locally, but when I pushed to GH it failed. I've spent a couple hours digging and trying to figure out what changed, but the whole remark/rehype/unified/hast ecosystem stuff is not super straightforward to me.
Can anyone tell if something has changed and I need to update the shape of the nodes coming out of this plugin?
Ok I finally figured it out. Between remark-html 14.0.0 and 14.0.1 they changed a default param.
https://github.com/torchlight-api/remark-torchlight/actions/runs/2506264156
The test now works, but I don't feel great that it relies on sanitize false.
Should this be a rehype plugin instead?
Amazing @aarondfrancis thank you! Will have a go with this later on.
RE: sanitize: false
I agree it feels incorrect, particularly after reading the remark docs.
RE: Rehype - in short, yes, I think you could be right. While it wasn't possible to use this library yesterday I ended up doing some digging of my own and integrating rehype-highlight
for SSR and started wondering the same thing.
The full pipeline felt like it made the most sense as a chain via unified. A torchlight-rehype
plugin could replace the use of rehype-highlight
below:
import {readFileSync} from 'fs'
import {unified} from 'unified'
import highlight from 'rehype-highlight'
import parse from 'remark-parse'
import rehype from 'remark-rehype'
import stringify from 'rehype-stringify'
import matter from 'gray-matter'
const markdown = matter(readFileSync(`./path/to/your/markdown.md`), 'utf8'))
const rendered = (await unified()
.use(parse)
.use(rehype)
.use(highlight)
.use(stringify)
.process(markdown.content))
.toString()
the whole remark/rehype/unified/hast ecosystem stuff is not super straightforward to me
...yep 🤯 - my takeaway was: remark for raw markdown content to HTML and rehype for processing HTML. They're all built on top of unified
.
@aarondfrancis it seems the last release that fixes this (0.0.3) is not published to NPM: https://www.npmjs.com/package/remark-torchlight?activeTab=versions
Any way to publish it to NPM? Thanks!
v0.0.3 might be unpublished because it requires a workaround that removes remark-html's sanitize option (creating an XSS attack vulnerabillity).
As a temporary solution, downloading v0.0.3 and providing it to package.json as a local module (i.e. in package.json reference the package via the syntax file:path/to/local-module
) works
It works like an 'allowlist' for HTML attributes and their values. I.e. classNames and styles. If a class or style is not explicitly provided, it will be removed.
To create a robust torchlight schema likely requires exporting some dictionaries from the core torchlight library (or from it's underlying shiki processor). For example, a map of all supported language keys.
I get this error when trying to import
remark-torchlight
:I reproduced the error in this CodeSandbox