-
-
Notifications
You must be signed in to change notification settings - Fork 377
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
using a transformer to add a wrapping element results in extra duplicative nodes #811
Comments
Make sure to return a function transformerTab(): ShikiTransformer {
return {
name: 'rehype-code:tab',
root(root) {
return {
type: 'root',
children: [
{
// anything you want ;)
children: root.children,
},
],
};
},
};
} |
Thanks for the correction. I see now that that's made clear in the types. Still looks like there isn't a way to get this to work with a root transformer https://stackblitz.com/edit/shikijs-shiki-issue-811-bwecfk?file=main.js. Feels like a bug but maybe I'm misunderstanding "root". And again a postprocess hook is really what I need, to have access to the rest of the fence. If anyone has a solution for that please share 🙏 (And if anyone finds this issue because you're trying to migrate from shikijs/twoslash/ |
oh you're right, I overlooked that. Markdown-it wraps the result from See markdown-it/markdown-it#269, this can be disabled with some configurations. Or the jsdoc of markdown-it: /**
* Highlighter function for fenced code blocks.
* Highlighter `function (str, lang, attrs)` should return escaped HTML. It can
* also return empty string if the source was not changed and should be escaped
* externally. If result starts with <pre... internal wrapper is skipped.
* @default null
*/
highlight?: ((str: string, lang: string, attrs: string) => string) | null | undefined;
} |
Great find! Okay now I have working implementation of I've proposed a change to markdown-it markdown-it/markdown-it#269 (comment), but the maintainer has said no to related ideas before. Maybe as a bandaid we could add my stackblitz's fence rule to the import shikiMarkdownItPlugin, { wrapperlessMarkdownItFenceRule } from '@shikijs/markdown-it';
import MarkdownIt from 'markdown-it';
const md = MarkdownIt();
md.renderer.rules.fence = wrapperlessMarkdownItFenceRule;
md.use(await shikiMarkdownItPlugin({
// …
transformers: // …,
})); |
I packaged up the wrapperless fence rule as a markdown-it plugin: https://github.com/olets/markdown-it-wrapperless-fence-rule The README has some Stackblitz links Would be happy to open a PR mentioning this plugin in the docs if maintainers are into it. |
Validations
Describe the bug
I'm working on a transformer which adds a root wrapper. Minimal goal is
I'm using
@shikijs/markdown-it
.root hook
My first thought was to use the
root
hook:Given
that transformer results in this HTML (I added the comments)
postprocess hook
Using a postprocess hook is a better fit for my need, because I'd like to use
options.meta
.That doesn't work either:
Given the same markdown as in the
root
hook example above, I get the same problematic result as above.Note that if we console log
html
in this hook, it does already have thepre > code
wrappers.Reproduction
https://stackblitz.com/edit/shikijs-shiki-issue-811
Contributes
The text was updated successfully, but these errors were encountered: