refact: move slugger logic to utils

This commit is contained in:
Jean-Philippe Sirois
2020-05-22 01:04:31 -04:00
parent d78096b5c6
commit 51fdbdeb28
2 changed files with 12 additions and 23 deletions
+2 -21
View File
@@ -1,5 +1,4 @@
import React, { FC, useMemo } from "react" import React, { FC } from "react"
import GithubSlugger from "github-slugger"
import { PageSidebarLink } from "../PageSidebarLink" import { PageSidebarLink } from "../PageSidebarLink"
import { ITocItem } from "../../types" import { ITocItem } from "../../types"
import * as SC from "./styles" import * as SC from "./styles"
@@ -24,27 +23,9 @@ function extractTitle(title: string): ITitle {
} }
} }
// needs to match the anchors generated by gatsby-remark-auto-headers-improved
// maybe extract logic
function cleanAnchors(items: ITocItem[]): ITocItem[] {
const slugger = new GithubSlugger()
slugger.reset()
const strip = (value: string) => value.replace(/^(\d*\-)(.*)/, "$2")
return items.map((item) => ({
...item,
link: `#${strip(slugger.slug(item.title))}`,
}))
}
export const Toc: FC<ITocProps> = ({ header, items }) => { export const Toc: FC<ITocProps> = ({ header, items }) => {
const windowGlobal = typeof window !== "undefined" && window const windowGlobal = typeof window !== "undefined" && window
const cleanedItems = useMemo(() => {
return cleanAnchors(items)
}, [items])
// window is not available on build // window is not available on build
if (!windowGlobal) { if (!windowGlobal) {
return null return null
@@ -54,7 +35,7 @@ export const Toc: FC<ITocProps> = ({ header, items }) => {
return ( return (
<SC.TocWrapper> <SC.TocWrapper>
{header} {header}
{cleanedItems.map((item) => { {items.map((item) => {
const { prefix, title } = extractTitle(item.title) const { prefix, title } = extractTitle(item.title)
return ( return (
+10 -2
View File
@@ -1,4 +1,4 @@
import kebabCase from "lodash/kebabCase" import GithubSlugger from "github-slugger"
import chain from "ramda/es/chain" import chain from "ramda/es/chain"
import partition from "ramda/es/partition" import partition from "ramda/es/partition"
import pipe from "ramda/es/pipe" import pipe from "ramda/es/pipe"
@@ -7,6 +7,8 @@ import sort from "ramda/es/sort"
import { IFile, IFileOrFolder, IFolder, ITocItem } from "../types" import { IFile, IFileOrFolder, IFolder, ITocItem } from "../types"
import { MarkdownRemark } from "../../generated/graphql" import { MarkdownRemark } from "../../generated/graphql"
const slugger = new GithubSlugger()
function specificWordsToUpper(str: string): string { function specificWordsToUpper(str: string): string {
const wordsToUpper = ["pdo", "c"] const wordsToUpper = ["pdo", "c"]
const toUpper = (word: string) => const toUpper = (word: string) =>
@@ -144,12 +146,18 @@ export function buildToc(headings: MarkdownRemark["headings"]): ITocItem[] {
return [] return []
} }
const strip = (value: string) => value.replace(/^(\d*\-)(.*)/, "$2")
slugger.reset()
return headings return headings
.filter((h) => h?.depth === 2) .filter((h) => h?.depth === 2)
.map((h) => { .map((h) => {
const slug = slugger.slug(h!.value!)
return { return {
depth: h!.depth!, depth: h!.depth!,
link: `#${kebabCase(h!.value!)}`, link: `#${strip(slug)}`,
title: h!.value!, title: h!.value!,
} }
}) })