This repository has been archived on 2022-08-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
outline/shared/utils/getHeadingsForText.js
2020-04-06 09:01:03 -07:00

29 lines
685 B
JavaScript

// @flow
import { filter } from 'lodash';
import slugify from 'shared/utils/slugify';
import unescape from 'shared/utils/unescape';
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 = unescape(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;
}