@ -1,6 +1,7 @@
|
|||||||
// @flow
|
// @flow
|
||||||
import { filter } from 'lodash';
|
import { filter } from 'lodash';
|
||||||
import slugify from 'shared/utils/slugify';
|
import slugify from 'shared/utils/slugify';
|
||||||
|
import unescape from 'shared/utils/unescape';
|
||||||
|
|
||||||
export default function getHeadingsForText(
|
export default function getHeadingsForText(
|
||||||
text: string
|
text: string
|
||||||
@ -13,7 +14,7 @@ export default function getHeadingsForText(
|
|||||||
if (!match) continue;
|
if (!match) continue;
|
||||||
|
|
||||||
const level = match[1].length;
|
const level = match[1].length;
|
||||||
const title = match[2];
|
const title = unescape(match[2]);
|
||||||
|
|
||||||
let slug = slugify(title);
|
let slug = slugify(title);
|
||||||
const existing = filter(output, { slug });
|
const existing = filter(output, { slug });
|
||||||
|
23
shared/utils/getHeadingsForText.test.js
Normal file
23
shared/utils/getHeadingsForText.test.js
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
/* eslint-disable flowtype/require-valid-file-annotation */
|
||||||
|
import getHeadingsForText from './getHeadingsForText';
|
||||||
|
|
||||||
|
it('should return an array of document headings', () => {
|
||||||
|
const response = getHeadingsForText(`
|
||||||
|
# Header
|
||||||
|
|
||||||
|
## Subheading
|
||||||
|
`);
|
||||||
|
|
||||||
|
expect(response.length).toBe(2);
|
||||||
|
expect(response[0].level).toBe(1);
|
||||||
|
expect(response[0].title).toBe('Header');
|
||||||
|
expect(response[1].level).toBe(2);
|
||||||
|
expect(response[1].title).toBe('Subheading');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should unescape special characters', () => {
|
||||||
|
const response = getHeadingsForText(`# Header <\\>`);
|
||||||
|
|
||||||
|
expect(response.length).toBe(1);
|
||||||
|
expect(response[0].title).toBe('Header <>');
|
||||||
|
});
|
Reference in New Issue
Block a user