|
| 1 | +const fs = require('fs'); |
| 2 | +const path = require('path'); |
| 3 | + |
| 4 | +module.exports = ({ source, template, options }) => { |
| 5 | + let match; |
| 6 | + const partials = []; |
| 7 | + let partialName = ''; |
| 8 | + let partialContent = ''; |
| 9 | + if (template !== null && options && options.partialsPath) { |
| 10 | + try { |
| 11 | + const partialPath = (options.partialsPath.trim() + '/').replace( |
| 12 | + '//', |
| 13 | + '/', |
| 14 | + ); |
| 15 | + const partialExt = options.partialsExt || '.f7p.html'; |
| 16 | + |
| 17 | + const externalPartialRegex = /{{>\s*["']([^"']+)["']\s*}}/gm; |
| 18 | + |
| 19 | + while ((match = externalPartialRegex.exec(template)) !== null) { |
| 20 | + // Avoid infinite loops with zero-width matches. |
| 21 | + if (match.index === externalPartialRegex.lastIndex) { |
| 22 | + externalPartialRegex.lastIndex += 1; |
| 23 | + } |
| 24 | + |
| 25 | + partialName = match[1]; |
| 26 | + |
| 27 | + try { |
| 28 | + const partialFilePath = path.resolve( |
| 29 | + partialPath + partialName + partialExt, |
| 30 | + ); |
| 31 | + |
| 32 | + if (fs.existsSync(partialFilePath)) { |
| 33 | + const file = fs.readFileSync(partialFilePath, 'utf8'); |
| 34 | + |
| 35 | + this.addDependency(partialFilePath); |
| 36 | + |
| 37 | + if (file.match(/<template[^>]*>/)) { |
| 38 | + partialContent = file |
| 39 | + .split(/<template[^>]*>/) |
| 40 | + .filter((item, index) => index > 0) |
| 41 | + .join('<template>') |
| 42 | + .split('</template>') |
| 43 | + .filter((item, index, arr) => index < arr.length - 1) |
| 44 | + .join('</template>') |
| 45 | + .replace(/{{#raw}}([ \n]*)<template/g, '{{#raw}}<template') |
| 46 | + .replace(/\/template>([ \n]*){{\/raw}}/g, '/template>{{/raw}}') |
| 47 | + .replace(/([ \n])<template/g, '$1{{#raw}}<template') |
| 48 | + .replace(/\/template>([ \n])/g, '/template>{{/raw}}$1') |
| 49 | + .replace(/(\r\n|\n|\r)/gm, '') |
| 50 | + .replace(/'/g, "\\'") |
| 51 | + .trim(); |
| 52 | + partials.push( |
| 53 | + `Template7.registerPartial('${partialName}', '${partialContent}');`, |
| 54 | + ); |
| 55 | + } |
| 56 | + } |
| 57 | + } catch (err) { |
| 58 | + console.log(err); |
| 59 | + } |
| 60 | + } |
| 61 | + } catch (err) { |
| 62 | + console.log(err); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + // Parse inline partials. |
| 67 | + const inlinePartialRegex = /<\s*template-partial[^>]*id="([^>]*)"[^>]*>([\s\S]*?)<\s*\/\s*template-partial>/gm; |
| 68 | + while ((match = inlinePartialRegex.exec(source)) !== null) { |
| 69 | + // Avoid infinite loops with zero-width matches. |
| 70 | + if (match.index === inlinePartialRegex.lastIndex) { |
| 71 | + inlinePartialRegex.lastIndex += 1; |
| 72 | + } |
| 73 | + partialName = match[1]; |
| 74 | + partialContent = match[2] |
| 75 | + // Remove newlines. |
| 76 | + .replace(/(\r\n|\n|\r)/gm, '') |
| 77 | + // Escape single quotes. |
| 78 | + .replace(/'/g, "\\'") |
| 79 | + // Trim whitespace |
| 80 | + .trim(); |
| 81 | + partials.push( |
| 82 | + `Template7.registerPartial('${partialName}', '${partialContent}');`, |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + return { partials }; |
| 87 | +}; |
0 commit comments