Val-istar-Guo / rehype-prism

The unified plugin used to highlight code block in html with Prism
MIT License
12 stars 1 forks source link

Bug: Toolbar doesn't work? #20

Closed BluDood closed 1 year ago

BluDood commented 1 year ago

Describe the bug I'm trying to add a toolbar with a Copy to Clipboard button to my parsed Markdown, but the toolbar and copy-to-clipboard plugins don't seem to do anything.

To Reproduce I use a parser function like this:

export async function parseMarkdown(md) {
  const html = await unified()
    .use(remarkParse)
    .use(remarkRehype, { allowDangerousHtml: true })
    .use(rehypePrism, { plugins: ['line-numbers', 'toolbar', 'copy-to-clipboard'] })
    .use(rehypeStringify)
    .process(md)
  return html.toString()
}

I have enabled the toolbar and copy-to-clipboard plugins, but they don't affect the output whatsoever.

Expected behavior The output should contain the toolbar section with the extra toolbar modules

Platform (please complete the following information):

BluDood commented 1 year ago

I am also using NextJS if that helps. I'm rendering the HTML inside getServerSideProps, and passing it down to the component. That shouldn't matter much though, as the output itself never has the toolbar element.

Val-istar-Guo commented 1 year ago

I review the code of toolbar plugin,It seems to only work on the browser rather than server.

Is there a demo project that can be used for testing, maybe we need to implement this plug-in, like line-numbers

BluDood commented 1 year ago

I have created a demo project here: BluDood/rehype-prism-test

Val-istar-Guo commented 1 year ago

https://github.com/PrismJS/prism/blob/59e5a3471377057de1f401ba38337aca27b80e03/plugins/toolbar/prism-toolbar.js#L3-L5

typeof document === 'undefined' is true on ssr. I have to rewrite this plugin....

Val-istar-Guo commented 1 year ago

https://github.com/PrismJS/prism/blob/59e5a3471377057de1f401ba38337aca27b80e03/plugins/copy-to-clipboard/prism-copy-to-clipboard.js#L115-L159

In order to support server-side rendering, we need inline js scripts to implement the copy function

<a href="#" onClick="(function(){
  // copy...
})();return false;">Copy</a>
Val-istar-Guo commented 1 year ago

install 2.2.2

BluDood commented 1 year ago

Thank you!