markdown-magic 2.6.1 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -10
- package/cli.js +5 -82
- package/lib/block-parser-js.test.js +179 -0
- package/lib/block-parser.js +389 -0
- package/lib/{utils/new-parser.test.js → block-parser.test.js} +168 -50
- package/lib/cli.js +234 -0
- package/lib/cli.test.js +409 -0
- package/lib/defaults.js +12 -0
- package/lib/index.js +319 -184
- package/lib/index.test.js +11 -0
- package/lib/options-parser.js +498 -0
- package/lib/options-parser.test.js +1237 -0
- package/lib/process-contents.js +330 -0
- package/lib/process-file.js +34 -0
- package/lib/transforms/code.js +67 -22
- package/lib/transforms/file.js +13 -10
- package/lib/transforms/remote.js +9 -6
- package/lib/transforms/toc.js +136 -64
- package/lib/transforms/wordCount.js +5 -0
- package/lib/utils/fs.js +340 -0
- package/lib/utils/fs.test.js +268 -0
- package/lib/utils/html-to-json/compat.js +42 -0
- package/lib/utils/html-to-json/format.js +64 -0
- package/lib/utils/html-to-json/index.js +37 -0
- package/lib/utils/html-to-json/lexer.js +345 -0
- package/lib/utils/html-to-json/parser.js +146 -0
- package/lib/utils/html-to-json/stringify.js +37 -0
- package/lib/utils/html-to-json/tags.js +171 -0
- package/lib/utils/index.js +19 -0
- package/{cli-utils.js → lib/utils/load-config.js} +2 -6
- package/lib/utils/logs.js +89 -0
- package/lib/utils/md/filters.js +20 -0
- package/lib/utils/md/find-code-blocks.js +80 -0
- package/lib/utils/md/find-date.js +32 -0
- package/lib/utils/md/find-frontmatter.js +94 -0
- package/lib/utils/md/find-frontmatter.test.js +17 -0
- package/lib/utils/md/find-html-tags.js +105 -0
- package/lib/utils/md/find-images.js +102 -0
- package/lib/utils/md/find-links.js +202 -0
- package/lib/utils/md/find-unmatched-html-tags.js +33 -0
- package/lib/utils/md/fixtures/2022-01-22-date-in-filename.md +14 -0
- package/lib/utils/md/fixtures/file-with-frontmatter.md +32 -0
- package/lib/utils/md/fixtures/file-with-links.md +143 -0
- package/lib/utils/md/md.test.js +37 -0
- package/lib/utils/md/parse.js +122 -0
- package/lib/utils/md/utils.js +19 -0
- package/lib/utils/regex-timeout.js +83 -0
- package/lib/utils/regex.js +38 -5
- package/lib/utils/remoteRequest.js +54 -0
- package/lib/utils/syntax.js +79 -0
- package/lib/utils/text.js +260 -0
- package/lib/utils/text.test.js +311 -0
- package/package.json +25 -25
- package/index.js +0 -46
- package/lib/processFile.js +0 -154
- package/lib/transforms/index.js +0 -114
- package/lib/updateContents.js +0 -125
- package/lib/utils/_md.test.js +0 -63
- package/lib/utils/new-parser.js +0 -412
- package/lib/utils/weird-parse.js +0 -230
- package/lib/utils/weird-parse.test.js +0 -217
package/lib/updateContents.js
DELETED
|
@@ -1,125 +0,0 @@
|
|
|
1
|
-
const regexUtils = require('./utils/regex')
|
|
2
|
-
|
|
3
|
-
/*
|
|
4
|
-
Update contents between Comment tags
|
|
5
|
-
*/
|
|
6
|
-
module.exports = async function updateContents(block, config) {
|
|
7
|
-
let newContent
|
|
8
|
-
const openingTag = getOpeningTags(block, config)
|
|
9
|
-
const closingTag = getClosingTags(block, config)
|
|
10
|
-
|
|
11
|
-
if (!openingTag.transform) {
|
|
12
|
-
// no transform command return original block
|
|
13
|
-
return block
|
|
14
|
-
}
|
|
15
|
-
const contentStart = openingTag.openTag.length
|
|
16
|
-
const endStart = block.indexOf(closingTag.closeTag, openingTag.openTag.length)
|
|
17
|
-
const originalContent = block.slice(contentStart, endStart).replace(/^\s+|\s+$/g, '')
|
|
18
|
-
|
|
19
|
-
if (openingTag.transform) {
|
|
20
|
-
const cmd = openingTag.transform.cmd
|
|
21
|
-
const cmdOptions = openingTag.transform.cmdOptions || {}
|
|
22
|
-
// check if command exists
|
|
23
|
-
if (cmd && config.transforms && config.transforms[cmd]) {
|
|
24
|
-
let updatedContent = await config.transforms[cmd](originalContent, cmdOptions, config)
|
|
25
|
-
if (typeof updatedContent === 'function') {
|
|
26
|
-
// if plugin has no options defined, invoke it with defaults
|
|
27
|
-
updatedContent = await updatedContent(originalContent, cmdOptions, config)
|
|
28
|
-
}
|
|
29
|
-
newContent = updatedContent
|
|
30
|
-
if (!newContent) {
|
|
31
|
-
console.log(`COMMAND '${cmd}' is returning undefined value. using original content instead. Make sure you return a value from your transform`)
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
if (!config.transforms[cmd]) {
|
|
35
|
-
console.warn(`Error '${cmd}' transform function not found in \`config.transforms\``)
|
|
36
|
-
console.warn(`Comment block skipped: <!-- ${config.matchWord}:START (${cmd}) -->`)
|
|
37
|
-
// throw new Error(errMsg)
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
// if no transform matches
|
|
42
|
-
if (!newContent) {
|
|
43
|
-
newContent = originalContent
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
// If original block or new content contains a new line, preserve it
|
|
47
|
-
if (block.indexOf('\n') > -1 || newContent.indexOf('\n') > -1) {
|
|
48
|
-
return `${openingTag.openTag}
|
|
49
|
-
${newContent}
|
|
50
|
-
${closingTag.closeTag}`
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
// Block has no newline, inline contents
|
|
54
|
-
return `${openingTag.openTag}${newContent}${closingTag.closeTag}`
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
function parseOptions(options) {
|
|
58
|
-
if (!options) {
|
|
59
|
-
return null
|
|
60
|
-
}
|
|
61
|
-
const returnOptions = {}
|
|
62
|
-
options.split('&').map((opt, i) => { // eslint-disable-line
|
|
63
|
-
const getValues = opt.split(/=(.+)/)
|
|
64
|
-
if (getValues[0] && getValues[1]) {
|
|
65
|
-
returnOptions[getValues[0]] = getValues[1]
|
|
66
|
-
}
|
|
67
|
-
})
|
|
68
|
-
return returnOptions
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
function processTransforms(hasCommand) {
|
|
72
|
-
const hasOptions = hasCommand[1].match(/([^:]*):(.*)/)
|
|
73
|
-
const cmd = (hasOptions) ? hasOptions[1] : hasCommand[1]
|
|
74
|
-
// no options found, run command with no options
|
|
75
|
-
const cmdOptions = (hasOptions) ? hasOptions[2] : null
|
|
76
|
-
return {
|
|
77
|
-
cmd: cmd,
|
|
78
|
-
cmdOptions: parseOptions(cmdOptions)
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
function getOpeningTags(block, config) {
|
|
83
|
-
const openTagRegex = regexUtils.matchOpeningCommentTag(config.matchWord)
|
|
84
|
-
let matches
|
|
85
|
-
while ((matches = openTagRegex.exec(block)) !== null) { // eslint-disable-line
|
|
86
|
-
// This is necessary to avoid infinite loops with zero-width matches
|
|
87
|
-
if (matches.index === openTagRegex.lastIndex) {
|
|
88
|
-
openTagRegex.lastIndex++
|
|
89
|
-
}
|
|
90
|
-
/*
|
|
91
|
-
console.log('FULL Open Tag >>>>>', matches[0])
|
|
92
|
-
console.log('openTag Start', "'"+matches[1]+"'");
|
|
93
|
-
console.log('openTag End', "'"+matches[2]+"'");
|
|
94
|
-
/**/
|
|
95
|
-
const hasCommand = matches[0].match(/\((.*)\)/)
|
|
96
|
-
const cmd = (hasCommand) ? processTransforms(hasCommand) : false
|
|
97
|
-
return {
|
|
98
|
-
openTag: matches[0],
|
|
99
|
-
openTagStart: matches[1],
|
|
100
|
-
openTagEnd: matches[2],
|
|
101
|
-
transform: cmd
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
function getClosingTags(block, config) {
|
|
107
|
-
const closeTagRegex = regexUtils.matchClosingCommentTag(config.matchWord)
|
|
108
|
-
let matches
|
|
109
|
-
while ((matches = closeTagRegex.exec(block)) !== null) { // eslint-disable-line
|
|
110
|
-
// This is necessary to avoid infinite loops with zero-width matches
|
|
111
|
-
if (matches.index === closeTagRegex.lastIndex) {
|
|
112
|
-
closeTagRegex.lastIndex++
|
|
113
|
-
}
|
|
114
|
-
/*
|
|
115
|
-
console.log('FULL CLOSE Tag >>>>>', matches[0])
|
|
116
|
-
console.log('closeTag Start', "'"+matches[1]+"'");
|
|
117
|
-
console.log('closeTag End', "'"+matches[2]+"'");
|
|
118
|
-
/**/
|
|
119
|
-
return {
|
|
120
|
-
closeTag: matches[1] + matches[2],
|
|
121
|
-
closeTagStart: matches[1],
|
|
122
|
-
closeTagEnd: matches[2]
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
}
|
package/lib/utils/_md.test.js
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
const { test } = require('uvu')
|
|
2
|
-
const assert = require('uvu/assert')
|
|
3
|
-
const { parseBlocks, replaceContent } = require('./new-parser')
|
|
4
|
-
|
|
5
|
-
const defaultOpts = {
|
|
6
|
-
syntax: 'md',
|
|
7
|
-
open: 'DOCS:START',
|
|
8
|
-
close: 'DOCS:END',
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
test('Returns empty array', () => {
|
|
12
|
-
assert.equal(parseBlocks('', defaultOpts).transforms, [])
|
|
13
|
-
assert.equal(parseBlocks(' ', defaultOpts).transforms, [])
|
|
14
|
-
assert.equal(parseBlocks(`
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
`, defaultOpts).transforms, [])
|
|
18
|
-
assert.equal(parseBlocks(`
|
|
19
|
-
# No block in here
|
|
20
|
-
|
|
21
|
-
nope
|
|
22
|
-
`, defaultOpts).transforms, [])
|
|
23
|
-
})
|
|
24
|
-
|
|
25
|
-
const md = `
|
|
26
|
-
Very nice
|
|
27
|
-
|
|
28
|
-
<!-- DOCS:START(TOC) foo={{ rad: 'orange' }} ------>
|
|
29
|
-
ok
|
|
30
|
-
<!-- DOCS:END -->`
|
|
31
|
-
|
|
32
|
-
test('Parse md blocks', () => {
|
|
33
|
-
const parsedValue = parseBlocks(md, defaultOpts)
|
|
34
|
-
console.log('parsedValue', parsedValue)
|
|
35
|
-
assert.equal(parsedValue.transforms, [
|
|
36
|
-
{
|
|
37
|
-
transform: 'TOC',
|
|
38
|
-
args: { foo: { rad: 'orange' } },
|
|
39
|
-
block: {
|
|
40
|
-
indentation: '',
|
|
41
|
-
start: 12,
|
|
42
|
-
end: 85,
|
|
43
|
-
contentStart: 64,
|
|
44
|
-
contentEnd: 68,
|
|
45
|
-
contentIndent: 0,
|
|
46
|
-
openTag: "<!-- DOCS:START(TOC) foo={{ rad: 'orange' }} ------>\n",
|
|
47
|
-
content: 'ok',
|
|
48
|
-
closeTag: '\n<!-- DOCS:END -->'
|
|
49
|
-
},
|
|
50
|
-
raw: {
|
|
51
|
-
transform: '(TOC)',
|
|
52
|
-
args: "foo={{ rad: 'orange' }} ----",
|
|
53
|
-
content: '\nok\n',
|
|
54
|
-
block: "<!-- DOCS:START(TOC) foo={{ rad: 'orange' }} ------>\n" +
|
|
55
|
-
'ok\n' +
|
|
56
|
-
'<!-- DOCS:END -->'
|
|
57
|
-
},
|
|
58
|
-
meta: { isMultiline: true }
|
|
59
|
-
}
|
|
60
|
-
], '')
|
|
61
|
-
})
|
|
62
|
-
|
|
63
|
-
test.run()
|
package/lib/utils/new-parser.js
DELETED
|
@@ -1,412 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
const weirdParse = require('./weird-parse')
|
|
3
|
-
|
|
4
|
-
const html = {
|
|
5
|
-
tags: ['<!--', '-->'],
|
|
6
|
-
pattern: ['<!--+', '-->'],
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
const jsx = {
|
|
10
|
-
tags: ['{/*', '*/}'],
|
|
11
|
-
pattern: [
|
|
12
|
-
'\{\/\\*+',
|
|
13
|
-
'\\*+/\}'
|
|
14
|
-
]
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
const yaml = {
|
|
18
|
-
tags: ['##', '##'],
|
|
19
|
-
pattern: [
|
|
20
|
-
'##+',
|
|
21
|
-
'##+'
|
|
22
|
-
],
|
|
23
|
-
converter: (str) => {
|
|
24
|
-
return str.split('\n').map((line) => {
|
|
25
|
-
return line[0] === '#' ? line : `#${line}`
|
|
26
|
-
}).join()
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
const syntaxMap = {
|
|
31
|
-
// <!-- x -->
|
|
32
|
-
md: html,
|
|
33
|
-
// <!-- x -->
|
|
34
|
-
html: html,
|
|
35
|
-
// /* x */
|
|
36
|
-
js: {
|
|
37
|
-
tags: ['/*', '*/'],
|
|
38
|
-
pattern: [
|
|
39
|
-
'\/\\*+', // '\/\*[\*\n\s\t]+', //
|
|
40
|
-
'\\*+/'
|
|
41
|
-
],
|
|
42
|
-
},
|
|
43
|
-
// {/* x */}
|
|
44
|
-
jsx: jsx,
|
|
45
|
-
mdx: jsx,
|
|
46
|
-
// ## x ##
|
|
47
|
-
yaml: yaml,
|
|
48
|
-
yml: yaml
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
function convertCommentSyntax({
|
|
52
|
-
str,
|
|
53
|
-
from,
|
|
54
|
-
to
|
|
55
|
-
}) {
|
|
56
|
-
const [ openPattern, closePattern ] = syntaxMap[from].pattern
|
|
57
|
-
const [ openTag, closeTag ] = syntaxMap[to].tags
|
|
58
|
-
const match = ` *?\\${openPattern}([\\s\\S]*?)?${closePattern}\\n\\n*?`
|
|
59
|
-
// const match = `${openPattern}(.*|\\r?|\\n?|\\s*)*${closePattern}`
|
|
60
|
-
const regexToUse = new RegExp(match, 'g')
|
|
61
|
-
// console.log('regexToUse', regexToUse)
|
|
62
|
-
const found = str.match(regexToUse)
|
|
63
|
-
if (!found) {
|
|
64
|
-
return str
|
|
65
|
-
}
|
|
66
|
-
const newComment = found[0].replace(regexToUse, `${openTag}$1${closeTag}`)
|
|
67
|
-
const converter = syntaxMap[to].converter
|
|
68
|
-
const newText = (converter) ? converter(newComment) : newComment
|
|
69
|
-
return str.replace(regexToUse, newText)
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
const defaultOptions = {
|
|
73
|
-
syntax: 'md',
|
|
74
|
-
open: `DOCS:START`,
|
|
75
|
-
close: `DOCS:END`,
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
function parseBlocks(contents, options = defaultOptions) {
|
|
79
|
-
const { syntax } = options
|
|
80
|
-
const transformsToRun = []
|
|
81
|
-
// const blockRegex = new RegExp(
|
|
82
|
-
// `(.*)(?:\\<\\!--(?:.*|\r?|\n?|\\s*)${matchWord}:START(?:(?:.|\\r?\\n)*?(?:\\()(.*)\\))?(.|\\r?\\n)*?)?<\!--(?:.*|\r?|\n?|\\s*)${matchWord}:END(?:.|\\r?\\n)*?--\\>`,
|
|
83
|
-
// 'gm'
|
|
84
|
-
// )
|
|
85
|
-
// `(?:.*)(?:\\<\\!--(?:.*|\\r?|\\n?|\\s*)${matchWord}:START\\s*([(\\[\\{]*[A-Za-z0-9_$-]*[)\\]\\}]*)\\s*)((?:.|\\r?\\n)*?)<\!--(?:.*|\\r?|\\n?|\\s*)${matchWord}:END(?:.|\\r?\\n)*?--\\>`,
|
|
86
|
-
const START = options.open
|
|
87
|
-
const END = options.close
|
|
88
|
-
|
|
89
|
-
const [ commentOpen, commentClose ] = syntaxMap[syntax].pattern
|
|
90
|
-
// const regexToUse = new RegExp(
|
|
91
|
-
// `([ \\t]*)(?:\\<\\!--(?:.*|\\r?|\\n?|\\s*)${matchWord}:START\\s*([(\\[\\{]*[A-Za-z0-9_$-]*[)\\]\\}]*)\\s*)((?:.|\\r?\\n)*?)<\!--(?:.*|\\r?|\\n?|\\s*)${matchWord}:END(?:.|\\r?\\n)*?--\\>`,
|
|
92
|
-
// 'gmi'
|
|
93
|
-
// )
|
|
94
|
-
const regexToUse = new RegExp(
|
|
95
|
-
`([ \\t]*)(?:${commentOpen}(?:.*|\\r?|\\n?|\\s*)${START}\\s*([(\\[\\{]*[A-Za-z0-9_$-]*[)\\]\\}]*)\\s*)((?:.|\\r?\\n)*?)${commentOpen}(?:.*|\\r?|\\n?|\\s*)${END}(?:.|\\r?\\n)*?${commentClose}`,
|
|
96
|
-
'gmi'
|
|
97
|
-
)
|
|
98
|
-
const paramsRegex = new RegExp(`([\\s\\S]*?)${commentClose}`, 'gm')
|
|
99
|
-
const trimRegex = new RegExp(`${commentClose}$`)
|
|
100
|
-
|
|
101
|
-
// console.log('paramsRegex', paramsRegex)
|
|
102
|
-
// ([ \t]*)(?:\/\*(?:.*|\r?|\n?|\s*)XYZ:START\s*([(\[\{]*[A-Za-z0-9_$-]*[)\]\}]*)\s*)((?:.|\r?\n)*?)\/\*(?:.*|\r?|\n?|\s*)XYZ:END(?:.|\r?\n)*?\*\/
|
|
103
|
-
// console.log('regexToUse', regexToUse)
|
|
104
|
-
|
|
105
|
-
let openTagRegex = matchOpeningCommentTag(START, commentOpen, commentClose)
|
|
106
|
-
let closeTagRegex = matchClosingCommentTag(END, commentOpen, commentClose)
|
|
107
|
-
while ((commentMatches = regexToUse.exec(contents)) !== null) {
|
|
108
|
-
let props = {}
|
|
109
|
-
let meta = {}
|
|
110
|
-
const [ block, spaces, action, params ] = commentMatches
|
|
111
|
-
const indentation = spaces || ''
|
|
112
|
-
/*
|
|
113
|
-
console.log('index', commentMatches.index)
|
|
114
|
-
console.log('block', block)
|
|
115
|
-
console.log('action', action)
|
|
116
|
-
console.log('params', params)
|
|
117
|
-
console.log('spaces', `"${spaces}"`)
|
|
118
|
-
/** */
|
|
119
|
-
// This is necessary to avoid infinite loops
|
|
120
|
-
if (commentMatches.index === regexToUse.lastIndex) {
|
|
121
|
-
regexToUse.lastIndex++
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
openTagRegex = matchOpeningCommentTag(START, commentOpen, commentClose)
|
|
125
|
-
const openingTag = getOpeningTags(block, {
|
|
126
|
-
pattern: openTagRegex,
|
|
127
|
-
open: commentOpen,
|
|
128
|
-
close: commentClose
|
|
129
|
-
})
|
|
130
|
-
closeTagRegex = matchClosingCommentTag(END, commentOpen, commentClose)
|
|
131
|
-
const closingTag = getClosingTags(block, {
|
|
132
|
-
pattern: closeTagRegex
|
|
133
|
-
})
|
|
134
|
-
/*
|
|
135
|
-
console.log('openingTag', openingTag)
|
|
136
|
-
console.log('closingTag', closingTag)
|
|
137
|
-
/** */
|
|
138
|
-
const openingTagLength = openingTag.length //+ indentation.length
|
|
139
|
-
const contentEndPosition = block.indexOf(closingTag.tag, openingTagLength)
|
|
140
|
-
const content = getTextBetween(block, openingTagLength, contentEndPosition)
|
|
141
|
-
// console.log('contentcontent', content)
|
|
142
|
-
let originalContent = content
|
|
143
|
-
const contentEndsWithNewLine = getLastCharacter(originalContent) === '\n'
|
|
144
|
-
const openEndsWithNewLine = getLastCharacter(openingTag.tag) === '\n'
|
|
145
|
-
const isMultiline = block.indexOf('\n') > -1
|
|
146
|
-
meta.isMultiline = isMultiline
|
|
147
|
-
const closeTag = (contentEndsWithNewLine) ? `\n${closingTag.tag}` : closingTag.tag
|
|
148
|
-
|
|
149
|
-
// Move new line to beginning of closing tag
|
|
150
|
-
// if (originalContent.match(/\n$/)) {
|
|
151
|
-
if (contentEndsWithNewLine) {
|
|
152
|
-
// originalContent = originalContent.replace(/\n$/, '')
|
|
153
|
-
originalContent = originalContent.slice(0, -1)
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
// Strip indentation
|
|
157
|
-
originalContent = stripIndent(originalContent, indentation.length)
|
|
158
|
-
// console.log('originalContent')
|
|
159
|
-
// console.log(`"${originalContent}"`)
|
|
160
|
-
// originalContent = originalContent.replace(/^\s+|\s+$/g, '')
|
|
161
|
-
|
|
162
|
-
// (functionName) or [functionName] or {functionName}
|
|
163
|
-
const transform = action.replace(/[(\[\{]*([A-Z-a-z0-9_$-]*)[)\]\}]*/, '$1')
|
|
164
|
-
// if (transform && !transform.match(/^-+/)) {
|
|
165
|
-
if (transform && getFirstCharacter(transform) !== '-') {
|
|
166
|
-
// console.log('params', params)
|
|
167
|
-
// const paramValue = params.match(/([\s\S]*?)-->/gm)
|
|
168
|
-
const paramValue = params.match(paramsRegex)
|
|
169
|
-
let paramString
|
|
170
|
-
if (paramValue) {
|
|
171
|
-
// paramString = paramValue[0].replace(/-*>$/, '').trim()
|
|
172
|
-
paramString = paramValue[0].replace(trimRegex, '').trim()
|
|
173
|
-
// console.log('paramString', paramString)
|
|
174
|
-
if (paramString) {
|
|
175
|
-
// Legacy v1 options parser
|
|
176
|
-
if (getFirstCharacter(paramString) === ':') {
|
|
177
|
-
meta.isLegacy = true
|
|
178
|
-
paramString = paramString.replace(/\s?\)\s?$/, '').substring(1)
|
|
179
|
-
props = legacyParseOptions(paramString)
|
|
180
|
-
} else {
|
|
181
|
-
props = weirdParse(paramString)
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
/*
|
|
186
|
-
console.log(regexToUse)
|
|
187
|
-
console.log(`transform "${transform}" at ${regexToUse.lastIndex} using props:`)
|
|
188
|
-
console.log(props)
|
|
189
|
-
console.log('───────────────────────')
|
|
190
|
-
/** */
|
|
191
|
-
const shift = (openEndsWithNewLine) ? 1 : 0
|
|
192
|
-
const contentStart = commentMatches.index + openingTag.tag.length - shift //+ indentation.length
|
|
193
|
-
const contentEnd = contentStart + content.length + indentation.length + shift
|
|
194
|
-
//const addOne = (contentEndsWithNewLine) ? 1 : 0
|
|
195
|
-
transformsToRun.push({
|
|
196
|
-
transform,
|
|
197
|
-
args: props,
|
|
198
|
-
// content: originalContent,
|
|
199
|
-
block: {
|
|
200
|
-
indentation,
|
|
201
|
-
start: commentMatches.index,
|
|
202
|
-
end: regexToUse.lastIndex,
|
|
203
|
-
contentStart,
|
|
204
|
-
contentEnd,
|
|
205
|
-
contentIndent: minIndent(originalContent),
|
|
206
|
-
openTag: openingTag.tag,
|
|
207
|
-
content: originalContent,
|
|
208
|
-
closeTag: closeTag,
|
|
209
|
-
// full: `${openingTag.tag}${indentString(originalContent, indentation.length)}${closeTag}`,
|
|
210
|
-
// full: indentString(`${openingTag.tag}${originalContent}${closeTag}`, indentation.length),
|
|
211
|
-
// full: indentString(`${stripIndent(openingTag.tag, indentation.length)}${originalContent}${stripIndent(closeTag, indentation.length)}`, indentation.length)
|
|
212
|
-
},
|
|
213
|
-
raw: {
|
|
214
|
-
transform: (meta.isLegacy) ? action.replace(/^\s?\(/, '') : action,
|
|
215
|
-
args: paramString,
|
|
216
|
-
content: getTextBetween(contents, contentStart, contentEnd),
|
|
217
|
-
block: block,
|
|
218
|
-
},
|
|
219
|
-
meta,
|
|
220
|
-
})
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
// const newer =
|
|
225
|
-
// /(?:.*)(?:\<\!--(?:.*|\r?|\n?|\s*)DOCS:START\s*([(\[\{][A-Z-a-z_$-]*[)\]\}])\s*)((?:.|\r?\n)*?)<\!--(?:.*|\r?|\n?|\s*)DOCS:END(?:.|\r?\n)*?--\>/gmi
|
|
226
|
-
// // console.log('newera', newer)
|
|
227
|
-
/*
|
|
228
|
-
const comments = content.match(newerString)
|
|
229
|
-
console.log('comments', comments)
|
|
230
|
-
const commentRegexInside = /<\!-*\s*([\s\S]*?) ?-*\>\n*?/g
|
|
231
|
-
if (comments) {
|
|
232
|
-
// console.log('comments', comments)
|
|
233
|
-
// console.log(comments.length)
|
|
234
|
-
comments.forEach((text) => {
|
|
235
|
-
console.log('text', text)
|
|
236
|
-
const inside = commentRegexInside.exec(text)
|
|
237
|
-
console.log('inside', inside)
|
|
238
|
-
if (inside) {
|
|
239
|
-
const config = inside[1].replace(`${matchWord}:START`, '').trim()
|
|
240
|
-
// console.log(formatProps(config))
|
|
241
|
-
}
|
|
242
|
-
})
|
|
243
|
-
}*/
|
|
244
|
-
|
|
245
|
-
return {
|
|
246
|
-
pattern: regexToUse,
|
|
247
|
-
commentOpen: openTagRegex,
|
|
248
|
-
commentClose: closeTagRegex,
|
|
249
|
-
transforms: transformsToRun
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
function replaceTextBetween(origin, startIndex, endIndex, insertion) {
|
|
254
|
-
return origin.substring(0, startIndex) + insertion + origin.substring(endIndex)
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
function replaceContent(origin, insertion, data) {
|
|
258
|
-
return replaceTextBetween(origin, data.block.contentStart, data.block.contentEnd, insertion)
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
function getFirstCharacter(str) {
|
|
262
|
-
return str.charAt(0)
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
function getLastCharacter(str) {
|
|
266
|
-
return str.substr(-1)
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
function getLeadingSpaces(text) {
|
|
270
|
-
return text.match(/^\s/) ? text : ''
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
function getTextBetween(text, start, end) {
|
|
274
|
-
return text.slice(start, end)
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
function stripIndent(string, indentation) {
|
|
278
|
-
const indent = typeof indentation !== 'undefined' ? indentation : minIndent(string);
|
|
279
|
-
if (indent === 0) {
|
|
280
|
-
return string
|
|
281
|
-
}
|
|
282
|
-
const regex = new RegExp(`^[ \\t]{${indent}}`, 'gm')
|
|
283
|
-
return string.replace(regex, '')
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
// https://github.com/jamiebuilds/min-indent/blob/master/index.js
|
|
287
|
-
function minIndent(string) {
|
|
288
|
-
const match = string.match(/^[ \t]*(?=\S)/gm)
|
|
289
|
-
if (!match) return 0
|
|
290
|
-
return match.reduce((r, a) => Math.min(r, a.length), Infinity)
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
function indentString(string, count = 1, options = {}) {
|
|
294
|
-
const {
|
|
295
|
-
indent = ' ',
|
|
296
|
-
includeEmptyLines = false
|
|
297
|
-
} = options;
|
|
298
|
-
if (count === 0) return string
|
|
299
|
-
const regex = includeEmptyLines ? /^/gm : /^(?!\s*$)/gm
|
|
300
|
-
return string.replace(regex, indent.repeat(count))
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
function getOpeningTags(block, {
|
|
304
|
-
pattern,
|
|
305
|
-
open,
|
|
306
|
-
close
|
|
307
|
-
}) {
|
|
308
|
-
// console.log(block.match(/^\/\*+(.*)\*\//))
|
|
309
|
-
// console.log('openTagRegex', pattern)
|
|
310
|
-
let matches
|
|
311
|
-
while ((matches = pattern.exec(block)) !== null) {
|
|
312
|
-
if (matches.index === pattern.lastIndex) {
|
|
313
|
-
pattern.lastIndex++ // avoid infinite loops with zero-width matches
|
|
314
|
-
}
|
|
315
|
-
const [ tag, spaces, tagStart, tagEnd ] = matches
|
|
316
|
-
/*
|
|
317
|
-
console.log('FULL Open Tag >>>>>', tag)
|
|
318
|
-
console.log('openTag Start', "'"+tagStart+"'");
|
|
319
|
-
console.log('openTag End', "'"+tagEnd+"'");
|
|
320
|
-
/**/
|
|
321
|
-
return {
|
|
322
|
-
tag,
|
|
323
|
-
spaces: spaces || '',
|
|
324
|
-
length: tag.length,
|
|
325
|
-
tagStart,
|
|
326
|
-
tagEnd,
|
|
327
|
-
}
|
|
328
|
-
}
|
|
329
|
-
// Fallthrough
|
|
330
|
-
const fallbackRegex = new RegExp(`^([ \\t]*)(${open}([\\s\\S]*?)${close})\\n?`)
|
|
331
|
-
// const xyz = block.match(/^([ \t]*)(\/\*+([\s\S]*?)\*+\/)/)
|
|
332
|
-
const xyz = block.match(fallbackRegex)
|
|
333
|
-
/*
|
|
334
|
-
console.log('fallbackRegex', fallbackRegex)
|
|
335
|
-
console.log('fall through', `"${block}"`)
|
|
336
|
-
console.log('xyz', xyz)
|
|
337
|
-
/** */
|
|
338
|
-
return {
|
|
339
|
-
tag: xyz[0],
|
|
340
|
-
spaces: xyz[1] || '',
|
|
341
|
-
length: xyz[0].length,
|
|
342
|
-
}
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
function getClosingTags(block, {
|
|
346
|
-
pattern,
|
|
347
|
-
// open,
|
|
348
|
-
// close
|
|
349
|
-
}) {
|
|
350
|
-
// console.log('closeTagRegex', closeTagRegex)
|
|
351
|
-
let matches
|
|
352
|
-
while ((matches = pattern.exec(block)) !== null) {
|
|
353
|
-
if (matches.index === pattern.lastIndex) {
|
|
354
|
-
pattern.lastIndex++ // avoid infinite loops with zero-width matches
|
|
355
|
-
}
|
|
356
|
-
const [ _tag, spaces, tagStart, tagEnd] = matches
|
|
357
|
-
/*
|
|
358
|
-
console.log('FULL CLOSE Tag >>>>>', matches[0])
|
|
359
|
-
console.log('closeTag Start', "'"+matches[1]+"'");
|
|
360
|
-
console.log('closeTag End', "'"+matches[2]+"'");
|
|
361
|
-
/**/
|
|
362
|
-
const tag = spaces + tagStart + tagEnd
|
|
363
|
-
return {
|
|
364
|
-
tag: tag,
|
|
365
|
-
length: tag.length,
|
|
366
|
-
spaces: spaces || '',
|
|
367
|
-
tagStart,
|
|
368
|
-
tagEnd
|
|
369
|
-
}
|
|
370
|
-
}
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
function removeComments(str) {
|
|
374
|
-
// /([^\s]*)?([ \\t]*)\<\!-+\s?([\s\S]*?)?-+\>\n*?([^\s]*)?/gi
|
|
375
|
-
const pattern = new RegExp(`([^\\s]*)?([ \\t]*)<!-+\\s?([\\s\\S]*?)?-+>\n*?([^\\s]*)?`, 'gi')
|
|
376
|
-
return str.replace(pattern, '')
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
function matchOpeningCommentTag(word, open, close) {
|
|
380
|
-
// console.log('open', open)
|
|
381
|
-
// return new RegExp(`(\\<\\!--(?:.|\\r?\\n)*?${matchWord}:START)((?:.|\\r?\\n)*?--\\>)`, 'g')
|
|
382
|
-
return new RegExp(`([ \\t]*)(${open}(?:.|\r?|\n?|\\s*)\\b${word}\\b)((?:.|\\r?\\n)*?${close}\n?)`, 'gi')
|
|
383
|
-
// return new RegExp(`([ \\t]*)(\\<\\!--(?:.*|\r?|\n?|\s*)${word}:START)((?:.|\\r?\\n)*?--\\>\n?)`, 'gi')
|
|
384
|
-
}
|
|
385
|
-
|
|
386
|
-
function matchClosingCommentTag(word, open, close) {
|
|
387
|
-
return new RegExp(`${close}(?:.|\\r?\\n)*?([ \t]*)((?:${open}(?:.*|\\r?\\n)(?:.*|\\r?\\n))*?\\b${word}\\b)((?:.|\\r?\\n)*?${close})`, 'gi')
|
|
388
|
-
// return new RegExp(`--\\>(?:.|\\r?\\n)*?([ \t]*)((?:\\<\\!--(?:.*|\\r?\\n)(?:.*|\\r?\\n))*?${word}:END)((?:.|\\r?\\n)*?--\\>)`, 'gi')
|
|
389
|
-
}
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
function legacyParseOptions(options) {
|
|
393
|
-
const returnOptions = {}
|
|
394
|
-
if (!options) {
|
|
395
|
-
return returnOptions
|
|
396
|
-
}
|
|
397
|
-
options.split('&').map((opt, i) => { // eslint-disable-line
|
|
398
|
-
const getValues = opt.split(/=(.+)/)
|
|
399
|
-
if (getValues[0] && getValues[1]) {
|
|
400
|
-
returnOptions[getValues[0]] = getValues[1]
|
|
401
|
-
}
|
|
402
|
-
})
|
|
403
|
-
return returnOptions
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
module.exports = {
|
|
408
|
-
getTextBetween,
|
|
409
|
-
replaceTextBetween,
|
|
410
|
-
replaceContent,
|
|
411
|
-
parseBlocks,
|
|
412
|
-
}
|