Skip to content

Commit

Permalink
Fix the IDs for headers
Browse files Browse the repository at this point in the history
  • Loading branch information
victorhmp committed Jul 29, 2019
1 parent 8c18636 commit 60aa9cd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
8 changes: 7 additions & 1 deletion react/components/CustomTags.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const CustomRenderers = {
},
emphasis: (props: any) => <em className="i">{props.children}</em>,
heading: (props: any) => {
const hashId = slug(props.children)
const hashId = getHeadingSlug(props.children)

switch (props.level) {
case 1:
Expand Down Expand Up @@ -169,3 +169,9 @@ export const CustomRenderers = {
tableRow: (props: any) => <tr className="bb b--muted-3">{props.children}</tr>,
thematicBreak: () => <hr className="mv7" />,
}

function getHeadingSlug(childNodes: any) {
return (
(childNodes[0].props.children && slug(childNodes[0].props.children)) || ''
)
}
4 changes: 2 additions & 2 deletions react/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import slugify from 'slugify'

function slug(str?: string | Record<string, any>) {
function slug(str: string) {
const replaced =
(str && str.toString().replace(/[*+~.()'"!:@&[\]]/g, '')) || ''
(typeof str === 'string' && str.replace(/[*+~.()'"!:@&[\]]/g, '')) || ''
const slugified = slugify(replaced, { lower: true }) || ''
return slugified
}
Expand Down

0 comments on commit 60aa9cd

Please sign in to comment.