Skip to content

Commit f894a9b

Browse files
Merge pull request #31 from cossssmin/feat/parser-options
Add support for customizing posthtml-parser
2 parents 22c4978 + 85aec08 commit f894a9b

File tree

4 files changed

+76
-2
lines changed

4 files changed

+76
-2
lines changed

.github/workflows/build.yml

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
2+
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
3+
4+
name: build
5+
6+
on:
7+
push:
8+
branches: [main]
9+
pull_request:
10+
11+
jobs:
12+
build:
13+
runs-on: ubuntu-latest
14+
15+
strategy:
16+
matrix:
17+
node-version: [14, 16, 18]
18+
19+
steps:
20+
- uses: actions/checkout@v2
21+
- name: Use Node.js ${{ matrix.node-version }}
22+
uses: actions/setup-node@v1
23+
with:
24+
node-version: ${{ matrix.node-version }}
25+
- run: npm install
26+
- run: npm test
27+
env:
28+
CI: true

readme.md

+33-1
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ You can run `npm run build` to compile them.
116116
| **propsContext** | `props` | String value used as object name inside the script to process process before passed to the component. |
117117
| **propsAttribute** | `props` | String value for props attribute to define props as JSON. |
118118
| **propsSlot** | `props` | String value used to retrieve the props passed to slot via `$slots.slotName.props`. |
119+
| **parserOptions** | `{recognizeSelfClosing: true}` | Object to configure `posthtml-parser`. By default, it enables support for self-closing component tags. |
119120
| **expressions** | `{}` | Object to configure `posthtml-expressions`. You can pre-set locals or customize the delimiters for example. |
120121
| **plugins** | `[]` | PostHTML plugins to apply for every parsed components. |
121122
| **matcher** | `[{tag: options.tagPrefix}]` | Array of object used to match the tags. |
@@ -216,6 +217,37 @@ Please see below example to understand better.
216217
<x-modal>Submit</x-modal>
217218
```
218219

220+
#### Parser options
221+
222+
You may pass options to `posthtml-parser` via `options.parserOptions`.
223+
224+
```js
225+
// index.js
226+
const options = {
227+
root: './src',
228+
parserOptions: { decodeEntities: true }
229+
};
230+
231+
require('posthtml')(require('posthtml-components')(options))
232+
.process('some HTML', options.parserOptions)
233+
.then(/* ... */)
234+
```
235+
236+
Important: as you can see, whatever `parserOptions` you pass to the plugin, must also be passed in the `process` method in your code, otherwise your PostHTML build will use `posthtml-parser` defaults and will override anything you've passed to `posthtml-component`.
237+
238+
#### Self-closing tags
239+
240+
The plugin supports self-closing tags by default, but you need to make sure to enable them in the `process` method in your code too, by passing `recognizeSelfClosing: true` in the options object:
241+
242+
```js
243+
// index.js
244+
require('posthtml')(require('posthtml-components')({root: './src'}))
245+
.process('your HTML...', {recognizeSelfClosing: true})
246+
.then(/* ... */)
247+
```
248+
249+
If you don't add this to `process`, PostHTML will use `posthtml-parser` defaults and will not support self-closing component tags. This will result in everything after a self-closing tag not being output.
250+
219251
### Multiple folders
220252

221253
You have full control where to place your components. Once you set the base root path of your components, you can then set multiple folders.
@@ -291,7 +323,7 @@ const options = {
291323

292324
Use the component namespace:
293325

294-
``` html
326+
```html
295327
<!-- src/index.html -->
296328
<html>
297329
<body>

src/index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ module.exports = (options = {}) => tree => {
5050
options.propsContext = options.propsContext || 'props';
5151
options.propsAttribute = options.propsAttribute || 'props';
5252
options.propsSlot = options.propsSlot || 'props';
53+
options.parserOptions = options.parserOptions || {recognizeSelfClosing: true};
5354
options.expressions = options.expressions || {};
5455
options.plugins = options.plugins || [];
5556
options.attrsParserRules = options.attrsParserRules || {};
@@ -180,7 +181,10 @@ function processTree(options) {
180181

181182
log(`${++processCounter}) Processing "${currentNode.tag}" from "${componentPath}"`, 'processTree');
182183

183-
let nextNode = parser(readFileSync(componentPath, 'utf8'));
184+
let nextNode = parser(
185+
readFileSync(componentPath, 'utf8'),
186+
mergeWith({recognizeSelfClosing: true}, options.parserOptions)
187+
);
184188

185189
// Set filled slots
186190
setFilledSlots(currentNode, filledSlots, options);

test/test-x-tag.js

+10
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,13 @@ test(`Must process component with namespace's custom path using index file`, asy
6767

6868
t.is(html, expected);
6969
});
70+
71+
test('Must process self-closing component x-tag', async t => {
72+
const actual = `<p>first</p><x-modal /><p>last</p>`;
73+
const expected = `<p>first</p><div>Modal</div><p>last</p>`;
74+
75+
const html = await posthtml([plugin({root: './test/templates', folders: 'components'})])
76+
.process(actual, {recognizeSelfClosing: true}).then(result => clean(result.html));
77+
78+
t.is(html, expected);
79+
});

0 commit comments

Comments
 (0)