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

Stop header tags having ids #389

Closed
EmmaB opened this issue May 24, 2022 · 2 comments
Closed

Stop header tags having ids #389

EmmaB opened this issue May 24, 2022 · 2 comments

Comments

@EmmaB
Copy link

EmmaB commented May 24, 2022

I am trying to stop header (h1 etc) tags having id and anchor attributes injected on preview. Using rehypeSanitize in previewOptions strips out a lot of clutter but I can't figure out how to stop them entirely. Many thanks for any insights, and thank you for this useful library.

I get:

<h1 id="user-content-bob"><a aria-hidden="true" tabindex="-1" href="#bob"></a>bob</h1>

I would like:

<h1>bob</h1>
@EmmaB
Copy link
Author

EmmaB commented May 24, 2022

Ah, this removes id, aria-hidden, tabindex and href.

...
        previewOptions={{
          remarkPlugins: [],
          rehypePlugins: [
            [
              rehypeSanitize,
              {
                ...defaultSchema,
                tagNames: ["h1"],
                clobber: [],
                attributes: {
                  "*": [null],
                },
              },
            ],
          ],
        }}

@EmmaB EmmaB closed this as completed May 24, 2022
@EmmaB
Copy link
Author

EmmaB commented May 31, 2022

oh. If you need to allow list a, that won't cut it. Instead I used rehype-rewrite:

        previewOptions={{
          remarkPlugins: [],
          rehypePlugins: [
            [
              rehypeRewrite,
              {
                rewrite: node => {
                  if (node.tagName == "h1") {
                    node.children = node.children.filter(
                      item => item.tagName != "a"
                    )
                  }
                },
              },
            ],
            [
              rehypeSanitize,
              {
                ...defaultSchema,
                //etc

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