Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to forward options to posthtml-expressions plugin #78

Merged
merged 3 commits into from
May 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,11 @@ function parseLocals({options, node}, optionLocals, attributeLocals) {

try {
const locals = merge({...optionLocals}, {...attrLocals}, JSON.parse(attributeLocals));

return expressions({locals});
return expressions({...options.expressions, locals});
} catch {
const locals = merge({...optionLocals}, {...attrLocals});

return expressions({locals});
return expressions({...options.expressions, locals});
}
}

Expand Down Expand Up @@ -135,6 +134,7 @@ module.exports = (options = {}) => {
options.attribute = options.attribute || 'href';
options.root = path.resolve(options.root || './');
options.attributeAsLocals = options.attributeAsLocals || false;
options.expressions = options.expressions || {};

return function (tree) {
if (options.initial) {
Expand Down
12 changes: 10 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ Root path for modules lookup.
Type: `array | function`\
Default: `[]`

PostHTML plugins to apply for every parsed module.
PostHTML plugins to apply for every parsed module.

If a function provided, it will be called with module's file path.

Expand Down Expand Up @@ -127,10 +127,18 @@ All attributes on `<module></module>` will be added to [locals](#locals)
Type: `object`\
Default: `{}`

Options for the PostHTML parser.
Options for the PostHTML parser.

By default, [`posthtml-parser`](https://github.com/posthtml/posthtml-parser) is used.

### `expressions`

Type: `object`\
Default: `{}`

Options to forward to [posthtml-expressions](https://github.com/posthtml/posthtml-expressions), like custom delimiters for example. Available options can be found [here](https://github.com/posthtml/posthtml-expressions#options).


## Component options

### `locals`
Expand Down
1 change: 1 addition & 0 deletions test/locals.expressions.spec.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div><i>%[ foo ]%</i><b>{{ ignored }}</b></div>
9 changes: 9 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,12 @@ test('Must parse attribute as locals', async t => {

t.is(html, expected);
});

test('Must use posthtml-expressions options', async t => {
const actual = `<module href="./test/locals.expressions.spec.html" locals='{"foo":"bar"}'></module>`;
const expected = `<div><i>bar</i><b>{{ ignored }}</b></div>`;

const html = await posthtml().use(plugin({expressions: {delimiters: ['%[', ']%']}})).process(actual).then(result => clean(result.html));

t.is(html, expected);
});