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

Commit

Permalink
improved regex code snippet capturing FIXES #19
Browse files Browse the repository at this point in the history
  • Loading branch information
alidcast committed Jul 23, 2017
1 parent 187ba88 commit c6cca69
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
19 changes: 13 additions & 6 deletions lib/content/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,24 @@ export default function prepPage (meta, options) {
return cached.permalink
},

get anchors () {
get anchors () { // TODO only for markdown files!
if (!cached.anchors) {
const level = options.anchorsLevel
const anchorsExp = new RegExp(`(#{${level + 1},})|#{${level}}(.*)`, 'g')

const anchorsExp = new RegExp([
'(`{3}[\\s\\S]*?`{3}|`{1}[^`].*?`{1}[^`])', // code snippet
`(#{${level + 1},})`, // other heading
`#{${level}}(.*)`, // heading text
].join('|'), 'g')

let result
let anchors = []
while (result = anchorsExp.exec(source)) {
let [match, otherLevel, capturedHeading] = result
if (!otherLevel && capturedHeading) {
const anchor = `#${paramCase(capturedHeading)}`
anchors.push([anchor, capturedHeading])
let [match, codeSnippet, otherHeading, headingText] = result
if (!(codeSnippet || otherHeading) && headingText) {
console.log('headng: ' + headingText)
const anchor = `#${paramCase(headingText)}`
anchors.push([anchor, headingText])
}
}
cached.anchors = anchors
Expand Down
12 changes: 8 additions & 4 deletions lib/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@ const mdCompParser = (mdParser) => {

const mdComponent = (source, moduleOpts, dirOpts) => {
const { srcPath, sitePath, componentsDir, parsers } = moduleOpts
// captures code or markdown component -- '@[]' or '@[]()'
const compExp = /(`{3}[a-z]*\n[\s\S]*?\n`{3})|(`{1}.*?`{1})|@\[(.*?)\](?:\((.*?)\))?/g

const compExp = new RegExp([
'(`{3}[\\s\\S]*?`{3}|`{1}[^`].*?`{1}[^`])', // code snippet
'@\\[(.*?)\\](?:\\((.*?)\\))?', // markdown component - '@[]' or '@[]()'
].join('|'), 'g')

let result
const comps = {}
while (result = compExp.exec(source)) {
let [match, inlineCode, codeBlock, name, props] = result
if (!inlineCode && !codeBlock) {
let [match, codeSnippet, name, props] = result
if (!codeSnippet) {
const compName = uppercamelcase(paramCase(name))
if (!comps[compName]) {
const basePath = join(sitePath, componentsDir, '/')
Expand Down

0 comments on commit c6cca69

Please sign in to comment.