UNPKG

3.91 kBJavaScriptView Raw
1'use strict';
2/*
3 Update contents between Comment tags
4*/
5const regexUtils = require('./utils/regex')
6
7module.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 // no transform command return original block
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 // check if command exists
25 if (cmd && config.transforms && config.transforms[cmd]) {
26 let updatedContent = config.transforms[cmd](originalContent, cmdOptions, config)
27 if (typeof updatedContent === 'function') {
28 // if plugin has no options defined, invoke it with defaults
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 // throw new Error(errMsg)
40 }
41 }
42
43 // if no transform matches
44 if (!newContent) {
45 newContent = originalContent
46 }
47
48 return `${openingTag.openTag}
49${newContent}
50${closingTag.closeTag}`
51}
52
53function parseOptions(options) {
54 if (!options) {
55 return null
56 }
57 const returnOptions = {}
58 options.split('&').map((opt, i) => { // eslint-disable-line
59 const getValues = opt.split(/=(.+)/)
60 if (getValues[0] && getValues[1]) {
61 returnOptions[getValues[0]] = getValues[1]
62 }
63 })
64 return returnOptions
65}
66
67function processTransforms(hasCommand) {
68 const hasOptions = hasCommand[1].match(/([^:]*):(.*)/)
69 const cmd = (hasOptions) ? hasOptions[1] : hasCommand[1]
70 // no options found, run command with no options
71 const cmdOptions = (hasOptions) ? hasOptions[2] : null
72 return {
73 cmd: cmd,
74 cmdOptions: parseOptions(cmdOptions)
75 }
76}
77
78function getOpeningTags(block, config) {
79 const openTagRegex = regexUtils.matchOpeningCommentTag(config.matchWord)
80 let matches
81 while ((matches = openTagRegex.exec(block)) !== null) { // eslint-disable-line
82 // This is necessary to avoid infinite loops with zero-width matches
83 if (matches.index === openTagRegex.lastIndex) {
84 openTagRegex.lastIndex++
85 }
86 /*
87 console.log('FULL Open Tag >>>>>', matches[0])
88 console.log('openTag Start', "'"+matches[1]+"'");
89 console.log('openTag End', "'"+matches[2]+"'");
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
102function getClosingTags(block, config) {
103 const closeTagRegex = regexUtils.matchClosingCommentTag(config.matchWord)
104 let matches
105 while ((matches = closeTagRegex.exec(block)) !== null) { // eslint-disable-line
106 // This is necessary to avoid infinite loops with zero-width matches
107 if (matches.index === closeTagRegex.lastIndex) {
108 closeTagRegex.lastIndex++
109 }
110 /*
111 console.log('FULL CLOSE Tag >>>>>', matches[0])
112 console.log('closeTag Start', "'"+matches[1]+"'");
113 console.log('closeTag End', "'"+matches[2]+"'");
114 /**/
115 return {
116 closeTag: matches[1] + matches[2],
117 closeTagStart: matches[1],
118 closeTagEnd: matches[2]
119 }
120 }
121}