Skip to content

Commit 746d2ab

Browse files
committed
feat: 🎸 add "markupTagName" option
Allow to customize the <tag> that is used to look for markup content
1 parent af573d0 commit 746d2ab

File tree

4 files changed

+44
-5
lines changed

4 files changed

+44
-5
lines changed

Diff for: ‎README.md

+8
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ _Note: If you want to load your `postcss` configuration from a external file, ma
4747

4848
### Template tag support
4949

50+
Add _vue-like_ support for defining your markup between a `<template>` tag. The tagname can be customized to something like `markup` for example. See [#options](#options).
51+
5052
_Note: only for auto preprocessing_
5153

5254
```html
@@ -317,6 +319,12 @@ svelte.preprocess(input, [
317319
const svelte = require('svelte')
318320
const sveltePreprocess = require('svelte-preprocess')
319321
const options = {
322+
/**
323+
* Define which tag should `svelte-preprocess` look for markup content.
324+
* This is only used if you desire to define your markup between this tag
325+
* or to import it from a external file.
326+
**/
327+
markupTagName: 'template',
320328
/**
321329
* Extend the default language alias dictionary.
322330
* Each entry must follow: ['alias', 'languageName']

Diff for: ‎src/autoProcess.js

+12-5
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,20 @@ const ALIAS_OPTION_OVERRIDES = {
1717
},
1818
}
1919

20-
const TEMPLATE_PATTERN = new RegExp(
21-
`<template([\\s\\S]*?)>([\\s\\S]*?)<\\/template>`,
22-
)
20+
module.exports = ({
21+
onBefore,
22+
aliases,
23+
markupTagName = 'template',
24+
preserve = [],
25+
...rest
26+
} = {}) => {
27+
markupTagName = markupTagName.toLocaleLowerCase()
2328

24-
module.exports = ({ onBefore, aliases, preserve = [], ...rest } = {}) => {
2529
const optionsCache = {}
2630
const transformers = rest.transformers || rest
31+
const markupPattern = new RegExp(
32+
`<${markupTagName}([\\s\\S]*?)>([\\s\\S]*)<\\/${markupTagName}>`,
33+
)
2734

2835
const getTransformerOptions = (lang, alias) => {
2936
if (isFn(transformers[alias])) return transformers[alias]
@@ -97,7 +104,7 @@ module.exports = ({ onBefore, aliases, preserve = [], ...rest } = {}) => {
97104
content = await onBefore({ content, filename })
98105
}
99106

100-
const templateMatch = content.match(TEMPLATE_PATTERN)
107+
const templateMatch = content.match(markupPattern)
101108

102109
/** If no <template> was found, just return the original markup */
103110
if (!templateMatch) {

Diff for: ‎test/autoProcess/markup.test.js

+11
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@ it('should transform HTML between <template></template>', async () => {
1616
)
1717
})
1818

19+
it('should transform HTML between custom tag <markup></markup>', async () => {
20+
const input = `<script></script><markup><div>Hey</div></markup><style></style>`
21+
const preprocessed = await preprocess(
22+
input,
23+
getAutoPreprocess({ markupTagName: 'markup' }),
24+
)
25+
expect(preprocessed.toString()).toBe(
26+
`<script></script>${EXPECTED_MARKUP}<style></style>`,
27+
)
28+
})
29+
1930
it('should transform a custom language between <template lang="..."></template>', async () => {
2031
const input = `<script></script><template lang="test"><div>Hey</div></template><style></style>`
2132
const preprocessed = await preprocess(

Diff for: ‎test/processors.test.js

+13
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const {
66
postcss,
77
coffeescript,
88
typescript,
9+
pug,
910
} = require('../src')
1011
const { CSS_PATTERN, getFixtureContent, preprocess } = require('./utils.js')
1112

@@ -22,6 +23,7 @@ const SCRIPT_LANGS = [
2223
['coffeescript', 'coffee', coffeescript],
2324
['typescript', 'ts', typescript, { compilerOptions: { module: 'es2015' } }],
2425
]
26+
const MARKUP_LANGS = [['pug', 'pug', pug]]
2527

2628
STYLE_LANGS.forEach(([lang, ext, processor, options]) => {
2729
describe(`processor - ${lang}`, () => {
@@ -42,3 +44,14 @@ SCRIPT_LANGS.forEach(([lang, ext, processor, options]) => {
4244
})
4345
})
4446
})
47+
48+
MARKUP_LANGS.forEach(([lang, ext, processor, options]) => {
49+
const EXPECTED_TEMPLATE = getFixtureContent('template.html')
50+
describe(`processor - ${lang}`, () => {
51+
it('should preprocess the whole file', async () => {
52+
const template = getFixtureContent('template.pug')
53+
const preprocessed = await preprocess(template, [processor(options)])
54+
expect(preprocessed.toString()).toContain(EXPECTED_TEMPLATE)
55+
})
56+
})
57+
})

0 commit comments

Comments
 (0)