|
"use strict"; |
|
Object.defineProperty(exports, "__esModule", { value: true }); |
|
exports.transformMarkup = exports.parseAttributes = exports.stripTags = exports.createTagRegex = void 0; |
|
|
|
function createTagRegex(tagName, flags) { |
|
return new RegExp(`/<!--[^]*?-->|<${tagName}(\\s[^]*?)?(?:>([^]*?)<\\/${tagName}>|\\/>)`, flags); |
|
} |
|
exports.createTagRegex = createTagRegex; |
|
|
|
function stripTags(markup) { |
|
return markup |
|
.replace(createTagRegex('style', 'gi'), '') |
|
.replace(createTagRegex('script', 'gi'), ''); |
|
} |
|
exports.stripTags = stripTags; |
|
|
|
function parseAttributes(attributesStr) { |
|
return attributesStr |
|
.split(/\s+/) |
|
.filter(Boolean) |
|
.reduce((acc, attr) => { |
|
const [name, value] = attr.split('='); |
|
|
|
acc[name] = value ? value.replace(/['"]/g, '') : true; |
|
return acc; |
|
}, {}); |
|
} |
|
exports.parseAttributes = parseAttributes; |
|
async function transformMarkup({ content, filename }, transformer, options = {}) { |
|
let { markupTagName = 'template' } = options; |
|
markupTagName = markupTagName.toLocaleLowerCase(); |
|
const markupPattern = createTagRegex(markupTagName); |
|
const templateMatch = content.match(markupPattern); |
|
|
|
if (!templateMatch || templateMatch.index == null) { |
|
return transformer({ |
|
content, |
|
markup: content, |
|
attributes: {}, |
|
filename, |
|
options, |
|
}); |
|
} |
|
const [fullMatch, attributesStr = '', templateCode] = templateMatch; |
|
const attributes = parseAttributes(attributesStr); |
|
|
|
let { code, map, dependencies } = await transformer({ |
|
content: templateCode, |
|
markup: templateCode, |
|
attributes, |
|
filename, |
|
options, |
|
}); |
|
code = |
|
content.slice(0, templateMatch.index) + |
|
code + |
|
content.slice(templateMatch.index + fullMatch.length); |
|
return { code, map, dependencies }; |
|
} |
|
exports.transformMarkup = transformMarkup; |
|
|