Skip to content

Commit

Permalink
Support non-alphanumeric characters in ToC
Browse files Browse the repository at this point in the history
```js
// Note: character range in regex is roughly "word characters including accented" (eg: bublé)
const id = text
  .toLowerCase()
  .replace(/[\s-]+/g, '-')
  .replace(/[^a-z0-9\u00C0-\u024F-]/g, '');
  // korean
  .replace(/[^가-힣]/g, '');
  // Japanese hiragana, katakana, kanji
  .replace(/[^\u3041-\u3096\u30A0-\u30FF\u2E80-\u2FD5]/g, '');
```

If want to keep existing intent, the above code might be more suitable.

Reference issue comment: preactjs#754 (comment)
  • Loading branch information
mu-hun committed Mar 6, 2021
1 parent 9d124dc commit 255c91b
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions src/lib/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,24 @@ export function getContent([lang, name]) {

let fallback = false;
const res = fetch(url)
.then(r => {
.then((r) => {
// fall back to english
if (!r.ok && lang != 'en') {
fallback = true;
return fetch(url.replace(/content\/[^/]+\//, 'content/en/'));
}
return r;
})
.then(r => {
.then((r) => {
if (r.ok) return r;
ext = 'md';
return fetch(`${path}/${r.status}.md`);
})
.then(r => r.text())
.then((r) => r.text())
.then(parseContent)
.then(applyEmojiToContent)
.then(parseMarkdownContent)
.then(data => {
.then((data) => {
data.fallback = fallback;
return data;
});
Expand Down Expand Up @@ -112,7 +112,7 @@ export function parseContent(text) {

const markedWorker = !PRERENDER && new MarkedWorker();
function parseMarkdownContent(data) {
return markedWorker.convert(data.content).then(html => {
return markedWorker.convert(data.content).then((html) => {
data.html = html;
return data;
});
Expand All @@ -121,7 +121,7 @@ function parseMarkdownContent(data) {
let processEmojis, pendingEmojiProcessor;

function applyEmojiToContent(data) {
return applyEmoji(data.content).then(content => {
return applyEmoji(data.content).then((content) => {
data.content = content;
return data;
});
Expand All @@ -144,7 +144,7 @@ function applyEmoji(content) {
/* webpackChunkName: "emoji" */ './gh-emoji'
).then(({ replace }) => (processEmojis = replace || String));
}
return pendingEmojiProcessor.then(processEmojis => processEmojis(content));
return pendingEmojiProcessor.then((processEmojis) => processEmojis(content));
}

/* generate ToC from markdown */
Expand All @@ -156,10 +156,7 @@ function generateToc(markdown) {
const level = token[1].length;
const text = sanitizeTitle(token[2]);
// Note: character range in regex is roughly "word characters including accented" (eg: bublé)
const id = text
.toLowerCase()
.replace(/[\s-]+/g, '-')
.replace(/[^a-z0-9\u00C0-\u024F-]/g, '');
const id = text.toLowerCase().replace(/[\s-]+/g, '-');
toc.push({ text, id, level });
}
return toc;
Expand Down

0 comments on commit 255c91b

Please sign in to comment.