-
Notifications
You must be signed in to change notification settings - Fork 9
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
Removes id when used with remarkStringify #11
Comments
Oh, d'oh.. just reading back through my question and I came up with the (much more straightforward!) solution: function remarkHeadingId() {
return (tree: Root) => {
visit(tree, 'heading', (node) => {
const text = node.children[0] as Text;
const match = text.value.match(/ {#([^]+?)}$/);
if (match !== null) {
node.data = {
hProperties: {
id: match[1],
},
hChildren: [
{
type: 'text',
value: text.value.slice(0, match.index),
},
],
};
}
});
};
} Instead of mutating the If this works for you I can create a PR? |
I can't understand the reason of the issue. Lines 25 to 29 in 5f6272e
Lines 20 to 29 in 5f6272e
|
Hi there, The difference is Lines 14 to 15 in 5f6272e
Whereas I propose using hChildren: [
{
type: 'text',
value: text.value.slice(0, match.index),
},
] If you are converting Markdown to HTML, you can transform the AST in 2 ways: you can directly change the Or the other way, you can use the special If you experiment with this example you should see that it currently removes the id attribute: import { unified, Processor } from 'unified';
import { Heading, Root, Text } from 'mdast';
import remarkParse from 'remark-parse';
import remarkStringify from 'remark-stringify';
const processor = unified()
.use(remarkParse)
.use(remarkHeadingId)
.use(remarkStringify)
const markdown = `### Hello {#hi}`;
console.log(String(await processor.process(markdown))) More info here. Thanks. |
@dmca-glasgow PR Welcomes! And please add some test cases. |
Hello,
I've been trying your package in my project, but I've found when I stringify my
mdast
tree back into markdown, the heading id is now lost.I've tried to hack together a solution which works for basic text titles, but I think it's an over-simplification of the problem and probably breaks a lot of stuff:
Transformed (position data removed):
Stringify Result:
### Hello {#hi}
With your plugin, the result is:
### Hello
I had a look at the way Micromark parses ATX headings to see if I could replicate the logic but it looks pretty complex!
Thanks.
The text was updated successfully, but these errors were encountered: