Skip to content

Commit

Permalink
fixture: make the logic of both non-English and English characters fi…
Browse files Browse the repository at this point in the history
…ltering with the same logic (nodejs#2570)

1. We should make sure that either your English anchor name or non-English
anchor name is filtered with the same logic, for non-English characters,
we should also include mid-spaces filtered out.
2. Move the ANCHOR_COMMENTREG out of the scope as the global-based one.
3. Remove the 'g', because it will remember the lastIndex where it began to
search for the last time.
  • Loading branch information
SEWeiTung authored Sep 14, 2019
1 parent 490cc43 commit 78503d8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
29 changes: 18 additions & 11 deletions scripts/plugins/anchor-markdown-headings.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,19 @@
* `<!---->` to quote your English anchor name inside, with your
* own title beside it.
*/
module.exports = function anchorMarkdownHeadings (text, level, raw, slugger) {
// Check whether we've got the comment matches
// <!--comment-->, <!--comment --> and even <!-- comment-->
// (20 hex = 32 dec = space character)
const ANCHOR_COMMENTREG = /<!--\x20?([\w|-]+)\x20?-->/gi

// Check whether we've got the comment matches
// <!--comment-->, <!--comment --> and even <!-- comment-->
// (20 hex = 32 dec = space character)
// Only need to find one that matches the Regex, we cannot use
// 'g' here because it will continue searching for the rest string,
// and you have to reset the lastIndex, which isn't a necessary for
// our current situation.
// For more, you can see:
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/lastIndex#Description
const ANCHOR_COMMENTREG = /<!--\x20?([\w\x20-]+)\x20?-->/

module.exports = function anchorMarkdownHeadings (text, level, raw, slugger) {
let anchorTitle = null

// If we've checked the title has a comment symbol,
Expand All @@ -27,15 +34,15 @@ module.exports = function anchorMarkdownHeadings (text, level, raw, slugger) {
if (anchorTitleArray !== null) {
anchorTitle = anchorTitleArray[1]
} else {
// For others, directly replace all non-English characters
// to '-' in the middle, only keep the English characters
anchorTitle = raw
.replace(/(\[([^\]]+)]\([^)]+\))/g, '$2')
.replace(/[^\w]+/g, '-')
.replace(/-{2,}/g, '-')
.replace(/(^-|-$)/g, '')
}

anchorTitle = anchorTitle.replace(/(\[([^\]]+)]\([^)]+\))/g, '$2')
.replace(/[^\w]+/g, '-')
.replace(/[\x20]+/g, '-')
.replace(/-{2,}/g, '-')
.replace(/(^-|-$)/g, '')

if (!anchorTitle) {
return `<h${level}>${text}</h${level}>`
}
Expand Down
17 changes: 16 additions & 1 deletion tests/scripts/anchor-mardown-headings.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const test = require('tape')
const anchorMarkdownHeadings = require('../../scripts/plugins/anchor-markdown-headings')

test('anchorMarkdownHeadings', (t) => {
t.plan(7)
t.plan(8)

t.test('correctly parses markdown heading without links', (t) => {
const text = 'Simple title'
Expand Down Expand Up @@ -76,6 +76,21 @@ test('anchorMarkdownHeadings', (t) => {
t.equal(output, expected)
})

t.test('correctly parses markdown heading with empty spaces', (t) => {
const text = '这是<a href="b">链接</a>的<a href="d">测试!</a>'
const level = 2
const raw = '<!-- empty spaces - id -->这是[链接](b)c[测试!](d)'
const output = anchorMarkdownHeadings(text, level, raw)
const expected = '<h2 id="header-empty-spaces-id">' +
'这是<a href="b">链接</a>的<a href="d">测试!</a>' +
'<a id="empty-spaces-id" class="anchor" ' +
'href="#empty-spaces-id" ' +
'aria-labelledby="header-empty-spaces-id"></a></h2>'

t.plan(1)
t.equal(output, expected)
})

t.test('does not generate empty anchors', (t) => {
const text = 'إنضم إلينا'
const level = 2
Expand Down

0 comments on commit 78503d8

Please sign in to comment.