-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(v2): add ability to set custom heading id (#4222)
* feat(v2): add ability to set custom heading id * Add cli command * Fix slugger * write-heading-ids doc + add in commands/templates * refactor + add tests for writeHeadingIds * polish writeHeadingIds * polish writeHeadingIds * remove i18n goals todo section as the remaining items are quite abstract/useless * fix edge case with 2 md links in heading * extract parseMarkdownHeadingId helper function * refactor using the shared parseMarkdownHeadingId utility fn * change logic of edge case * Handle edge case * Document explicit ids feature Co-authored-by: slorber <lorber.sebastien@gmail.com>
- Loading branch information
Showing
26 changed files
with
594 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
packages/docusaurus-mdx-loader/src/remark/headings/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
/* Based on remark-slug (https://github.com/remarkjs/remark-slug) and gatsby-remark-autolink-headers (https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby-remark-autolink-headers) */ | ||
|
||
const {parseMarkdownHeadingId} = require('@docusaurus/utils'); | ||
const visit = require('unist-util-visit'); | ||
const toString = require('mdast-util-to-string'); | ||
const slugs = require('github-slugger')(); | ||
|
||
function headings() { | ||
const transformer = (ast) => { | ||
slugs.reset(); | ||
|
||
function visitor(headingNode) { | ||
const data = headingNode.data || (headingNode.data = {}); // eslint-disable-line | ||
const properties = data.hProperties || (data.hProperties = {}); | ||
let {id} = properties; | ||
|
||
if (id) { | ||
id = slugs.slug(id, true); | ||
} else { | ||
const headingTextNodes = headingNode.children.filter( | ||
({type}) => !['html', 'jsx'].includes(type), | ||
); | ||
const heading = toString( | ||
headingTextNodes.length > 0 | ||
? {children: headingTextNodes} | ||
: headingNode, | ||
); | ||
|
||
// Support explicit heading IDs | ||
const parsedHeading = parseMarkdownHeadingId(heading); | ||
|
||
id = parsedHeading.id || slugs.slug(heading); | ||
|
||
if (parsedHeading.id) { | ||
// When there's an id, it is always in the last child node | ||
// Sometimes heading is in multiple "parts" (** syntax creates a child node): | ||
// ## part1 *part2* part3 {#id} | ||
const lastNode = | ||
headingNode.children[headingNode.children.length - 1]; | ||
|
||
if (headingNode.children.length > 1) { | ||
const lastNodeText = parseMarkdownHeadingId(lastNode.value).text; | ||
// When last part contains test+id, remove the id | ||
if (lastNodeText) { | ||
lastNode.value = lastNodeText; | ||
} | ||
// When last part contains only the id: completely remove that node | ||
else { | ||
headingNode.children.pop(); | ||
} | ||
} else { | ||
lastNode.value = parsedHeading.text; | ||
} | ||
} | ||
} | ||
|
||
data.id = id; | ||
properties.id = id; | ||
} | ||
|
||
visit(ast, 'heading', visitor); | ||
}; | ||
|
||
return transformer; | ||
} | ||
|
||
module.exports = headings; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.