Fix html in header IDs

This commit is contained in:
Aaron Carlino 2019-11-14 15:05:03 +13:00
parent 64d05765fc
commit 9485472792
5 changed files with 22 additions and 6 deletions

View File

@ -1,6 +1,7 @@
{ {
"workbench.colorCustomizations": { "workbench.colorCustomizations": {
"activityBar.background": "#88d7ea", "activityBar.background": "#88d7ea",
"activityBar.activeBorder": "#de41bf",
"activityBar.foreground": "#15202b", "activityBar.foreground": "#15202b",
"activityBar.inactiveForeground": "#15202b99", "activityBar.inactiveForeground": "#15202b99",
"activityBarBadge.background": "#de41bf", "activityBarBadge.background": "#de41bf",

View File

@ -0,0 +1,8 @@
const cleanHeaders = (html: string): string => {
return html.replace(
/<h([0-9])>(.*?)(\{#.*?<\/?em>.*?})/g,
(_, level, title, tag) => `<h${level}>${title} ${tag.replace(/<\/?em>/g, '_')}`
);
};
export default cleanHeaders;

View File

@ -118,7 +118,7 @@ const getHomePage = (): SilverstripeDocument | null => {
return __home; return __home;
}; };
const getCurrentVersion = (): string | null => __currentVersion; const getCurrentVersion = (): string => __currentVersion || '4';
const setCurrentNode = (slug: string): void => { const setCurrentNode = (slug: string): void => {
const currentNode = getNodes().find(n => n.slug === slug) || null; const currentNode = getNodes().find(n => n.slug === slug) || null;

View File

@ -8,6 +8,8 @@ import cleanCalloutTags from './cleanCalloutTags';
import { ReactElement } from 'react'; import { ReactElement } from 'react';
import rewriteTable from './rewriteTable'; import rewriteTable from './rewriteTable';
import rewriteHeader from './rewriteHeader'; import rewriteHeader from './rewriteHeader';
import cleanHeaders from './cleanHeaders';
/** /**
* Replace all the [CHILDREN] with proper React components. * Replace all the [CHILDREN] with proper React components.
* @param html * @param html
@ -19,7 +21,8 @@ const parseHTML = (html: string): ReactElement | ReactElement[] | string => {
cleanHTML = cleanCalloutTags(cleanHTML); cleanHTML = cleanCalloutTags(cleanHTML);
cleanHTML = cleanWhitespace(cleanHTML); cleanHTML = cleanWhitespace(cleanHTML);
cleanHTML = cleanApiTags(cleanHTML); cleanHTML = cleanApiTags(cleanHTML);
cleanHTML = cleanHeaders(cleanHTML);
const parseOptions = { const parseOptions = {
replace(domNode: DomElement): ReactElement | object | undefined | false { replace(domNode: DomElement): ReactElement | object | undefined | false {
const { name, attribs, children } = domNode; const { name, attribs, children } = domNode;

View File

@ -2,7 +2,7 @@ import { createElement, ReactElement } from 'react';
import { domToReact, DomElement, HTMLReactParserOptions } from 'html-react-parser'; import { domToReact, DomElement, HTMLReactParserOptions } from 'html-react-parser';
import { Link } from 'gatsby'; import { Link } from 'gatsby';
import rewriteAPILink from './rewriteAPILink'; import rewriteAPILink from './rewriteAPILink';
import { getCurrentNode } from '../utils/nodes'; import { getCurrentNode, getCurrentVersion } from '../utils/nodes';
import path from 'path'; import path from 'path';
interface LinkAttributes { interface LinkAttributes {
@ -21,7 +21,8 @@ const rewriteLink = (
} }
const currentNode = getCurrentNode(); const currentNode = getCurrentNode();
const version = getCurrentVersion();
// api links // api links
if (href.match(/^api\:/)) { if (href.match(/^api\:/)) {
const newHref = rewriteAPILink(href); const newHref = rewriteAPILink(href);
@ -42,10 +43,13 @@ const rewriteLink = (
} }
// Relative to root // Relative to root
if (href.substring(0, 1) === '/') { if (href.startsWith('/')) {
return createElement( return createElement(
Link, Link,
{ to: href, className: 'gatsby-link' }, {
to: path.join('en', version, href),
className: 'gatsby-link'
},
domToReact(children, parseOptions) domToReact(children, parseOptions)
) )
} }