1 | 'use strict';
|
2 |
|
3 |
|
4 |
|
5 | const regexUtils = require('./utils/regex')
|
6 |
|
7 | module.exports = function updateContents(block, config) {
|
8 | let newContent
|
9 | const openingTag = getOpeningTags(block, config)
|
10 | const closingTag = getClosingTags(block, config)
|
11 |
|
12 | if (!openingTag.transform) {
|
13 |
|
14 | return block
|
15 | }
|
16 | const contentStart = openingTag.openTag.length
|
17 | const endStart = block.indexOf(closingTag.closeTag, openingTag.openTag.length)
|
18 | const originalContent = block.slice(contentStart, endStart).replace(/^\s+|\s+$/g, '')
|
19 |
|
20 | if (openingTag.transform) {
|
21 | const cmd = openingTag.transform.cmd
|
22 | const cmdOptions = openingTag.transform.cmdOptions
|
23 |
|
24 |
|
25 | if (cmd && config.transforms && config.transforms[cmd]) {
|
26 | let updatedContent = config.transforms[cmd](originalContent, cmdOptions, config)
|
27 | if (typeof updatedContent === 'function') {
|
28 |
|
29 | updatedContent = updatedContent(originalContent, cmdOptions, config)
|
30 | }
|
31 | newContent = updatedContent
|
32 | if (!newContent) {
|
33 | console.log(`COMMAND '${cmd}' is returning undefined value. using original content instead. Make sure you return a value from your transform`)
|
34 | }
|
35 | }
|
36 | if (!config.transforms[cmd]) {
|
37 | console.warn(`Error '${cmd}' transform function not found in \`config.transforms\``)
|
38 | console.warn(`Comment block skipped: <!-- ${config.matchWord}:START (${cmd}) -->`)
|
39 |
|
40 | }
|
41 | }
|
42 |
|
43 |
|
44 | if (!newContent) {
|
45 | newContent = originalContent
|
46 | }
|
47 |
|
48 | return `${openingTag.openTag}
|
49 | ${newContent}
|
50 | ${closingTag.closeTag}`
|
51 | }
|
52 |
|
53 | function parseOptions(options) {
|
54 | if (!options) {
|
55 | return null
|
56 | }
|
57 | const returnOptions = {}
|
58 | options.split('&').map((opt, i) => {
|
59 | const getValues = opt.split(/=(.+)/)
|
60 | if (getValues[0] && getValues[1]) {
|
61 | returnOptions[getValues[0]] = getValues[1]
|
62 | }
|
63 | })
|
64 | return returnOptions
|
65 | }
|
66 |
|
67 | function processTransforms(hasCommand) {
|
68 | const hasOptions = hasCommand[1].match(/([^:]*):(.*)/)
|
69 | const cmd = (hasOptions) ? hasOptions[1] : hasCommand[1]
|
70 |
|
71 | const cmdOptions = (hasOptions) ? hasOptions[2] : null
|
72 | return {
|
73 | cmd: cmd,
|
74 | cmdOptions: parseOptions(cmdOptions)
|
75 | }
|
76 | }
|
77 |
|
78 | function getOpeningTags(block, config) {
|
79 | const openTagRegex = regexUtils.matchOpeningCommentTag(config.matchWord)
|
80 | let matches
|
81 | while ((matches = openTagRegex.exec(block)) !== null) {
|
82 |
|
83 | if (matches.index === openTagRegex.lastIndex) {
|
84 | openTagRegex.lastIndex++
|
85 | }
|
86 | |
87 |
|
88 |
|
89 |
|
90 |
|
91 | const hasCommand = matches[0].match(/\((.*)\)/)
|
92 | const cmd = (hasCommand) ? processTransforms(hasCommand) : false
|
93 | return {
|
94 | openTag: matches[0],
|
95 | openTagStart: matches[1],
|
96 | openTagEnd: matches[2],
|
97 | transform: cmd
|
98 | }
|
99 | }
|
100 | }
|
101 |
|
102 | function getClosingTags(block, config) {
|
103 | const closeTagRegex = regexUtils.matchClosingCommentTag(config.matchWord)
|
104 | let matches
|
105 | while ((matches = closeTagRegex.exec(block)) !== null) {
|
106 |
|
107 | if (matches.index === closeTagRegex.lastIndex) {
|
108 | closeTagRegex.lastIndex++
|
109 | }
|
110 | |
111 |
|
112 |
|
113 |
|
114 |
|
115 | return {
|
116 | closeTag: matches[1] + matches[2],
|
117 | closeTagStart: matches[1],
|
118 | closeTagEnd: matches[2]
|
119 | }
|
120 | }
|
121 | }
|