Skip to content

Commit

Permalink
Fix iterative#955: Generate correct slug for titles with md
Browse files Browse the repository at this point in the history
  • Loading branch information
iAdramelk committed Jan 29, 2020
1 parent 7c6b4ba commit 724919d
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/Documentation/Markdown/Markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,16 @@ function flatten(text, child) {
: React.Children.toArray(child.props.children).reduce(flatten, text)
}

const SLUG_REGEXP = /\s+{#([a-z0-9-]*[a-z0-9]+)}\s*$/

function isTitleHasSlug(title) {
return typeof title === 'string' && SLUG_REGEXP.test(title)
}

export function extractSlugFromTitle(title) {
// extracts expressions like {#too-many-files} from the end of a title
const meta = title.match(/\s+{#([a-z0-9-]*[a-z0-9]+)}\s*$/)
const meta = title.match(SLUG_REGEXP)

if (meta) {
return [title.substring(0, meta.index), meta[1]]
}
Expand All @@ -58,7 +65,7 @@ const HeadingRenderer = ({ level, children }) => {
let slug = kebabCase(text)

const lastElement = content[content.length - 1].props.children
if (typeof lastElement === 'string') {
if (isTitleHasSlug(lastElement)) {
const [newValue, newSlug] = extractSlugFromTitle(lastElement)
content.push(React.cloneElement(content.pop(), [], newValue))
slug = newSlug
Expand Down

0 comments on commit 724919d

Please sign in to comment.