-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Fixed an issue where inline footnotes were copied to title tag a…
…nd section id when heading was defined
- Loading branch information
1 parent
146abea
commit 59f8db0
Showing
6 changed files
with
145 additions
and
156 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/** | ||
* derived from `remark-slug`. | ||
* original: Copyright (c) 2015 Titus Wormer <tituswormer@gmail.com> | ||
* modified: 2021 and later is Akabeko | ||
* @license MIT | ||
* @see https://github.com/remarkjs/remark-slug | ||
*/ | ||
|
||
import GithubSlugger from 'github-slugger'; | ||
import toString from 'mdast-util-to-string'; | ||
import { Node } from 'unist'; | ||
import { selectAll } from 'unist-util-select'; | ||
|
||
/** | ||
* Heading in Markdown AST. | ||
*/ | ||
interface Heading extends Node { | ||
children: Node[]; | ||
data?: { | ||
id?: string; | ||
hProperties?: { | ||
id?: string; | ||
}; | ||
}; | ||
} | ||
|
||
/** | ||
* Create slug from `id` or heading children. | ||
* @param heading Heading. | ||
* @param slugs Slugger. | ||
* @returns | ||
*/ | ||
const createSlug = (heading: Heading, slugger: GithubSlugger) => { | ||
if (heading.data && heading.data.hProperties && heading.data.hProperties.id) { | ||
return slugger.slug(heading.data.hProperties.id, true); | ||
} | ||
|
||
// Create slug string with footnotes removed | ||
const children = [...heading.children]; | ||
heading.children = heading.children.filter( | ||
(child: Node) => child.type !== 'footnote', | ||
); | ||
const text = slugger.slug(toString(heading)); | ||
heading.children = children; | ||
|
||
return text; | ||
}; | ||
|
||
/** | ||
* Process Markdown AST. | ||
* @returns Transformer. | ||
*/ | ||
export const mdast = () => (tree: any) => { | ||
const slugger = new GithubSlugger(); | ||
slugger.reset(); | ||
|
||
const headings = selectAll('heading', tree) as Heading[]; | ||
for (const heading of headings) { | ||
const id = createSlug(heading, slugger); | ||
if (!heading.data) { | ||
heading.data = {}; | ||
} | ||
|
||
if (!heading.data.hProperties) { | ||
heading.data.hProperties = {}; | ||
} | ||
|
||
heading.data.id = id; | ||
heading.data.hProperties.id = id; | ||
} | ||
}; |
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.