mirror of
https://github.com/silverstripe/doc.silverstripe.org
synced 2024-10-01 05:39:40 +02:00
26 lines
743 B
TypeScript
26 lines
743 B
TypeScript
|
import { createElement, ReactElement } from "react";
|
||
|
import { DomElement } from 'html-react-parser';
|
||
|
|
||
|
const rewriteHeaders = (domNode: DomElement): ReactElement | false => {
|
||
|
if (!domNode.name) {
|
||
|
return false;
|
||
|
}
|
||
|
const firstChild = domNode.children ? domNode.children[0] : null;
|
||
|
if (firstChild && firstChild.type === 'text') {
|
||
|
const { data } = firstChild;
|
||
|
const matches = data.match(/^(.*?){#([A-Za-z0-9_-]+)\}$/);
|
||
|
if (matches) {
|
||
|
const header = matches[1]
|
||
|
const id = matches[2];
|
||
|
return createElement(
|
||
|
domNode.name,
|
||
|
{ id },
|
||
|
header
|
||
|
);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
export default rewriteHeaders;
|