Skip to content
This repository was archived by the owner on Feb 2, 2021. It is now read-only.

Commit c6cca69

Browse files
committed
improved regex code snippet capturing FIXES #19
1 parent 187ba88 commit c6cca69

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

lib/content/page.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,24 @@ export default function prepPage (meta, options) {
3636
return cached.permalink
3737
},
3838

39-
get anchors () {
39+
get anchors () { // TODO only for markdown files!
4040
if (!cached.anchors) {
4141
const level = options.anchorsLevel
42-
const anchorsExp = new RegExp(`(#{${level + 1},})|#{${level}}(.*)`, 'g')
42+
43+
const anchorsExp = new RegExp([
44+
'(`{3}[\\s\\S]*?`{3}|`{1}[^`].*?`{1}[^`])', // code snippet
45+
`(#{${level + 1},})`, // other heading
46+
`#{${level}}(.*)`, // heading text
47+
].join('|'), 'g')
48+
4349
let result
4450
let anchors = []
4551
while (result = anchorsExp.exec(source)) {
46-
let [match, otherLevel, capturedHeading] = result
47-
if (!otherLevel && capturedHeading) {
48-
const anchor = `#${paramCase(capturedHeading)}`
49-
anchors.push([anchor, capturedHeading])
52+
let [match, codeSnippet, otherHeading, headingText] = result
53+
if (!(codeSnippet || otherHeading) && headingText) {
54+
console.log('headng: ' + headingText)
55+
const anchor = `#${paramCase(headingText)}`
56+
anchors.push([anchor, headingText])
5057
}
5158
}
5259
cached.anchors = anchors

lib/loader.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,17 @@ const mdCompParser = (mdParser) => {
1818

1919
const mdComponent = (source, moduleOpts, dirOpts) => {
2020
const { srcPath, sitePath, componentsDir, parsers } = moduleOpts
21-
// captures code or markdown component -- '@[]' or '@[]()'
22-
const compExp = /(`{3}[a-z]*\n[\s\S]*?\n`{3})|(`{1}.*?`{1})|@\[(.*?)\](?:\((.*?)\))?/g
21+
22+
const compExp = new RegExp([
23+
'(`{3}[\\s\\S]*?`{3}|`{1}[^`].*?`{1}[^`])', // code snippet
24+
'@\\[(.*?)\\](?:\\((.*?)\\))?', // markdown component - '@[]' or '@[]()'
25+
].join('|'), 'g')
26+
2327
let result
2428
const comps = {}
2529
while (result = compExp.exec(source)) {
26-
let [match, inlineCode, codeBlock, name, props] = result
27-
if (!inlineCode && !codeBlock) {
30+
let [match, codeSnippet, name, props] = result
31+
if (!codeSnippet) {
2832
const compName = uppercamelcase(paramCase(name))
2933
if (!comps[compName]) {
3034
const basePath = join(sitePath, componentsDir, '/')

0 commit comments

Comments
 (0)