* feat: New table of contents * fix: Hide TOC in edit mode * feat: Highlight follows scroll position * scroll tracking * UI * fix: Unrelated css fix with long doc titles * Improve responsiveness * feat: Add keyboard shortcut access to TOC * fix: Headings should reflect content correctly when viewing old document revision * flow * fix: Persist TOC choice between sessions
28 lines
629 B
JavaScript
28 lines
629 B
JavaScript
// @flow
|
|
import { filter } from 'lodash';
|
|
import slugify from 'shared/utils/slugify';
|
|
|
|
export default function getHeadingsForText(
|
|
text: string
|
|
): { level: number, title: string, slug: string }[] {
|
|
const regex = /^(#{1,6})\s(.*)$/gm;
|
|
|
|
let match;
|
|
let output = [];
|
|
while ((match = regex.exec(text)) !== null) {
|
|
if (!match) continue;
|
|
|
|
const level = match[1].length;
|
|
const title = match[2];
|
|
|
|
let slug = slugify(title);
|
|
const existing = filter(output, { slug });
|
|
if (existing.length) {
|
|
slug = `${slug}-${existing.length}`;
|
|
}
|
|
output.push({ level, title, slug });
|
|
}
|
|
|
|
return output;
|
|
}
|