Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to support advanced features with code highlighting #30

Closed
zce opened this issue Jan 25, 2024 · 1 comment
Closed

Unable to support advanced features with code highlighting #30

zce opened this issue Jan 25, 2024 · 1 comment

Comments

@zce
Copy link
Owner

zce commented Jan 25, 2024

TL;DR

At present, some common advanced features of code highlighting plug-ins cannot work properly, such as highlighted lines.

Explains

Because the most of code-highlighting plug-ins implement these advanced features by code blocks meta, like this:

```js {1,3-4}
// highlighted-line
// 
console.log('foo') // highlighted-line
console.log('bar') // highlighted-line
```

But in Velite's markdown process, rehype-raw is used to process the rehype tree before loading the custom rehypePlugins.

However, rehype-raw will lead to the loss of metadata, refer to:

@zce
Copy link
Owner Author

zce commented Jan 25, 2024

import { unified } from 'unified'
import rehypeRaw from 'rehype-raw'
import remarkParse from 'remark-parse'
import remarkRehype from 'remark-rehype'
import rehypeStringify from 'rehype-stringify'
import rehypePrettyCode from 'rehype-pretty-code'
import { inspect } from 'unist-util-inspect'

const text = `
<img src="https://example.com/image.png" alt="Example image" />

\`\`\`js {1}
import React from 'react'
\`\`\`
`

const file = await unified()
  .use(remarkParse)
  .use(remarkRehype, { allowDangerousHtml: true })
  .use(() => tree => console.log('before rehypeRaw', inspect(tree), '\n'))
  .use(rehypeRaw)
  .use(() => tree => console.log('after rehypeRaw', inspect(tree), '\n'))
  .use(rehypePrettyCode)
  .use(rehypeStringify)
  .process(text)

console.log(file.toString())

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant