mirror of
https://github.com/silverstripe/doc.silverstripe.org
synced 2024-10-22 15:05:50 +00:00
Header IDs, smooth scrolling, smaller code font
This commit is contained in:
parent
181fef9e99
commit
04eefa3e37
@ -12,4 +12,26 @@ if (typeof window !== "undefined") {
|
|||||||
|
|
||||||
exports.wrapPageElement = ({ element, props }) => {
|
exports.wrapPageElement = ({ element, props }) => {
|
||||||
return <Layout {...props}>{element}</Layout>
|
return <Layout {...props}>{element}</Layout>
|
||||||
};
|
};
|
||||||
|
|
||||||
|
exports.onRouteUpdate = ({location}) => {
|
||||||
|
anchorScroll(location);
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
exports.shouldUpdateScroll = ({
|
||||||
|
routerProps: { location },
|
||||||
|
}) => {
|
||||||
|
anchorScroll(location);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
const anchorScroll = location => {
|
||||||
|
// Check for location so build does not fail
|
||||||
|
if (location && location.hash) {
|
||||||
|
setTimeout(() => {
|
||||||
|
const item = document.querySelector(`${location.hash}`).offsetTop;
|
||||||
|
const mainNavHeight = document.querySelector(`header`).offsetHeight;
|
||||||
|
window.scrollTo({top: item - mainNavHeight, left: 0, behavior: 'smooth'});
|
||||||
|
}, 0);
|
||||||
|
}
|
||||||
|
}
|
@ -205,6 +205,10 @@ h1, h2, h3 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
code[class*="language-"], pre[class*="language-"] {
|
||||||
|
font-size: 0.8rem;
|
||||||
|
}
|
||||||
|
|
||||||
:not(pre) > code[class*="language-"] {
|
:not(pre) > code[class*="language-"] {
|
||||||
background: #f5f6f8;
|
background: #f5f6f8;
|
||||||
color: #5d6778;
|
color: #5d6778;
|
||||||
|
@ -1,6 +1,16 @@
|
|||||||
import { createElement, ReactElement } from "react";
|
import { createElement, ReactElement } from "react";
|
||||||
import { DomElement } from 'html-react-parser';
|
import { DomElement } from 'html-react-parser';
|
||||||
|
|
||||||
|
const generateID = (title: string): string => {
|
||||||
|
return title
|
||||||
|
.replace('&', '-and-')
|
||||||
|
.replace('&', '-and-')
|
||||||
|
.replace(/[^A-Za-z0-9]/g, '-')
|
||||||
|
.replace(/-+/g, '-')
|
||||||
|
.replace(/^-/g, '')
|
||||||
|
.replace(/-$/g, '')
|
||||||
|
.toLowerCase();
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* If a header has a {#explicit-id}, add it as an attribute
|
* If a header has a {#explicit-id}, add it as an attribute
|
||||||
* @param domNode
|
* @param domNode
|
||||||
@ -13,18 +23,26 @@ const rewriteHeaders = (domNode: DomElement): ReactElement | false => {
|
|||||||
if (firstChild && firstChild.type === 'text') {
|
if (firstChild && firstChild.type === 'text') {
|
||||||
const { data } = firstChild;
|
const { data } = firstChild;
|
||||||
const matches = data.match(/^(.*?){#([A-Za-z0-9_-]+)\}$/);
|
const matches = data.match(/^(.*?){#([A-Za-z0-9_-]+)\}$/);
|
||||||
|
let header;
|
||||||
|
let id;
|
||||||
if (matches) {
|
if (matches) {
|
||||||
const header = matches[1]
|
header = matches[1];
|
||||||
const id = matches[2];
|
id = matches[2];
|
||||||
return createElement(
|
} else {
|
||||||
domNode.name,
|
header = data;
|
||||||
{ id },
|
id = generateID(data);
|
||||||
header
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return createElement(
|
||||||
|
domNode.name,
|
||||||
|
{ id },
|
||||||
|
header
|
||||||
|
);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
export default rewriteHeaders;
|
export default rewriteHeaders;
|
Loading…
x
Reference in New Issue
Block a user