From 9485472792ba64e30bb5590c4d06c2d735aeabac Mon Sep 17 00:00:00 2001 From: Aaron Carlino Date: Thu, 14 Nov 2019 15:05:03 +1300 Subject: [PATCH] Fix html in header IDs --- .vscode/settings.json | 1 + src/utils/cleanHeaders.ts | 8 ++++++++ src/utils/nodes.ts | 2 +- src/utils/parseHTML.ts | 5 ++++- src/utils/rewriteLink.ts | 12 ++++++++---- 5 files changed, 22 insertions(+), 6 deletions(-) create mode 100644 src/utils/cleanHeaders.ts diff --git a/.vscode/settings.json b/.vscode/settings.json index 20e237a..85b7898 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,6 +1,7 @@ { "workbench.colorCustomizations": { "activityBar.background": "#88d7ea", + "activityBar.activeBorder": "#de41bf", "activityBar.foreground": "#15202b", "activityBar.inactiveForeground": "#15202b99", "activityBarBadge.background": "#de41bf", diff --git a/src/utils/cleanHeaders.ts b/src/utils/cleanHeaders.ts new file mode 100644 index 0000000..8fa0e55 --- /dev/null +++ b/src/utils/cleanHeaders.ts @@ -0,0 +1,8 @@ +const cleanHeaders = (html: string): string => { + return html.replace( + /(.*?)(\{#.*?<\/?em>.*?})/g, + (_, level, title, tag) => `${title} ${tag.replace(/<\/?em>/g, '_')}` + ); +}; + +export default cleanHeaders; \ No newline at end of file diff --git a/src/utils/nodes.ts b/src/utils/nodes.ts index ebd6fd6..12faba8 100644 --- a/src/utils/nodes.ts +++ b/src/utils/nodes.ts @@ -118,7 +118,7 @@ const getHomePage = (): SilverstripeDocument | null => { return __home; }; -const getCurrentVersion = (): string | null => __currentVersion; +const getCurrentVersion = (): string => __currentVersion || '4'; const setCurrentNode = (slug: string): void => { const currentNode = getNodes().find(n => n.slug === slug) || null; diff --git a/src/utils/parseHTML.ts b/src/utils/parseHTML.ts index 0c53265..0d6c48e 100644 --- a/src/utils/parseHTML.ts +++ b/src/utils/parseHTML.ts @@ -8,6 +8,8 @@ import cleanCalloutTags from './cleanCalloutTags'; import { ReactElement } from 'react'; import rewriteTable from './rewriteTable'; import rewriteHeader from './rewriteHeader'; +import cleanHeaders from './cleanHeaders'; + /** * Replace all the [CHILDREN] with proper React components. * @param html @@ -19,7 +21,8 @@ const parseHTML = (html: string): ReactElement | ReactElement[] | string => { cleanHTML = cleanCalloutTags(cleanHTML); cleanHTML = cleanWhitespace(cleanHTML); cleanHTML = cleanApiTags(cleanHTML); - + cleanHTML = cleanHeaders(cleanHTML); + const parseOptions = { replace(domNode: DomElement): ReactElement | object | undefined | false { const { name, attribs, children } = domNode; diff --git a/src/utils/rewriteLink.ts b/src/utils/rewriteLink.ts index ab50a68..e0bb879 100644 --- a/src/utils/rewriteLink.ts +++ b/src/utils/rewriteLink.ts @@ -2,7 +2,7 @@ import { createElement, ReactElement } from 'react'; import { domToReact, DomElement, HTMLReactParserOptions } from 'html-react-parser'; import { Link } from 'gatsby'; import rewriteAPILink from './rewriteAPILink'; -import { getCurrentNode } from '../utils/nodes'; +import { getCurrentNode, getCurrentVersion } from '../utils/nodes'; import path from 'path'; interface LinkAttributes { @@ -21,7 +21,8 @@ const rewriteLink = ( } const currentNode = getCurrentNode(); - + const version = getCurrentVersion(); + // api links if (href.match(/^api\:/)) { const newHref = rewriteAPILink(href); @@ -42,10 +43,13 @@ const rewriteLink = ( } // Relative to root - if (href.substring(0, 1) === '/') { + if (href.startsWith('/')) { return createElement( Link, - { to: href, className: 'gatsby-link' }, + { + to: path.join('en', version, href), + className: 'gatsby-link' + }, domToReact(children, parseOptions) ) }