From 51d3b9b02ba77540022cfdc6aaf2c22e3298e3db Mon Sep 17 00:00:00 2001 From: Roman Hotsiy Date: Sun, 7 Jul 2019 21:26:27 +0300 Subject: [PATCH] fix: broken headings with single quote fixes #955 --- src/services/MarkdownRenderer.ts | 5 +++-- src/utils/helpers.ts | 4 ++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/services/MarkdownRenderer.ts b/src/services/MarkdownRenderer.ts index 0e08c6c4b5..d95b3bc35c 100644 --- a/src/services/MarkdownRenderer.ts +++ b/src/services/MarkdownRenderer.ts @@ -1,6 +1,6 @@ import * as marked from 'marked'; -import { highlight, safeSlugify } from '../utils'; +import { highlight, safeSlugify, unescapeHTMLChars } from '../utils'; import { AppStore } from './AppStore'; import { RedocNormalizedOptions } from './RedocNormalizedOptions'; @@ -65,6 +65,7 @@ export class MarkdownRenderer { container: MarkdownHeading[] = this.headings, parentId?: string, ): MarkdownHeading { + name = unescapeHTMLChars(name); const item = { id: parentId ? `${parentId}/${safeSlugify(name)}` : `section/${safeSlugify(name)}`, name, @@ -88,7 +89,7 @@ export class MarkdownRenderer { } attachHeadingsDescriptions(rawText: string) { - const buildRegexp = heading => { + const buildRegexp = (heading: MarkdownHeading) => { return new RegExp(`##?\\s+${heading.name.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')}`); }; diff --git a/src/utils/helpers.ts b/src/utils/helpers.ts index bfe8da946c..cb90dc97fa 100644 --- a/src/utils/helpers.ts +++ b/src/utils/helpers.ts @@ -194,3 +194,7 @@ function parseURL(url: string) { return new URL(url); } } + +export function unescapeHTMLChars(str: string): string { + return str.replace(/&#(\d+);/g, (_m, code) => String.fromCharCode(parseInt(code, 10))); +}