Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit will add anchor links to Markdown and MDX headings. The first step is to generate heading IDs that can be used in the links. Astro does this automatically for Markdown and MDX content. Astro exports `{ rehypeHeadingIds } from "@astrojs/markdown-remark"`, which can be imported into the Astro config file. In the `rehypePlugins` array, `rehypeHeadingIds` must be specified first before other plugins that use `rehypeHeadingIds`. https://docs.astro.build/en/guides/markdown-content/ The next step is to use the heading IDs to create links. Astro supports Rehype and Remark plugins, and the `rehype-autolink-headings` plugin can be used to create heading links. This commit will install the plugin, import it into the Astro config file, and add it to the `rehypePlugins` array after `rehypeHeadingIds`. https://github.com/rehypejs/rehype-autolink-headings The final step is to structure and style the heading links. Heading links can be separated from headings by displaying elements adjacent to the headings, as GitHub does in READMEs and other Markdown content. Link elements can contain text, such as an octothorpe (the "pound sign" `#`) or emoji (🔗), or an icon font. SVG images can also be used in heading link elements, but additional considerations are involved. The SVG icon code itself needs to be added, either by pasting the SVG string into a JavaScript or CSS file, by storing the SVG as an external file and importing in a JavaScript or CSS file, or by building SVG nodes programmatically with a library like https://github.com/syntax-tree/hast (used in the Rehype/Remark/unifiedjs ecosystem projects). The approach taken here is to store the SVG in an external file and import with CSS. The link icon is from the same source as the other icons on the site, Phosphor Icons (https://phosphoricons.com/, MIT license). The icon then needs to be positioned in the DOM. GitHub approaches this by placing the icon in the left margin. On small viewports like mobile devices, the link icons are always visible. On larger viewports, the link icons are hidden by default, then revealed when hovering over any part of the heading or icon. A similar approach is taken here, with some modifications to account for the pre-existing styles on this site. Size units are similar to https://github.com/unifiedjs/unifiedjs.github.io. For a more exact replication of GitHub's styling, see: https://github.com/rehypejs/rehype-github https://github.com/sindresorhus/github-markdown-css Future work may add wrapper elements to improve heading link structure. Wrapping heading elements in anchor elements, or conversely placing anchor elements inside headings, can lead to accessibility limitations. The parent-child element structure may not be clear to screen readers. An alternative approach is to make the heading element and link element siblings (at the same level in the HTML document) and then add a wrapper element around both sibling elements. Again, this is what GitHub does. https://amberwilson.co.uk/blog/are-your-anchor-links-accessible/
- Loading branch information