posthtml-component 2.2.2 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -55,7 +55,7 @@ If you are familiar with Blade, React, Vue or similar, you will find the syntax
55
55
  | **propsSlot** | `String` | `'props'` | Used to retrieve props passed to slot via `$slots.slotName.props`. |
56
56
  | **parserOptions** | `Object` | `{recognizeSelfClosing: true}` | Pass options to `posthtml-parser`. |
57
57
  | **expressions** | `Object` | `{}` | Pass options to `posthtml-expressions`. |
58
- | **plugins** | `Array` | `[]` | PostHTML plugins to apply to every parsed component. |
58
+ | **plugins** | `Array\|Object` | `[]` | PostHTML plugins to [apply](#plugins) to every parsed component. |
59
59
  | **matcher** | `Object` | `[{tag: options.tagPrefix}]` | Array of objects used to match tags. |
60
60
  | **attrsParserRules** | `Object` | `{}` | Additional rules for attributes parser plugin. |
61
61
  | **strict** | `Boolean` | `true` | Toggle exception throwing. |
@@ -842,6 +842,30 @@ Result:
842
842
 
843
843
  You can add custom rules to define how attributes are parsed - we use [posthtml-attrs-parser](https://github.com/posthtml/posthtml-attrs-parser) to handle them.
844
844
 
845
+
846
+ ### Plugins
847
+
848
+ Use the `plugins` option to apply your own PostHTML plugins to the components.
849
+
850
+ This can be an array of plugins, or an object with `before` and `after` properties to apply plugins before or after the `posthtml-expressions` plugin runs.
851
+
852
+ ```js
853
+ // As array
854
+ const options = {
855
+ plugins: [/* ... */]
856
+ };
857
+
858
+ // As object with `before` and `after`
859
+ const options = {
860
+ plugins: {
861
+ before: [/* ... */],
862
+ after: [/* ... */],
863
+ }
864
+ };
865
+ ```
866
+
867
+ When using `plugins` as an array, the plugins will be applied after `posthtml-expressions`.
868
+
845
869
  ### Advanced attributes configurations
846
870
 
847
871
  If default configurations for valid attributes are not right for you, you may configure them as explained below.
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "posthtml-component",
3
- "version": "2.2.2",
3
+ "version": "2.3.0",
4
4
  "description": "Laravel Blade-inspired components for PostHTML with slots, attributes as props, custom tags and more.",
5
5
  "license": "MIT",
6
6
  "repository": "posthtml/posthtml-components",
7
7
  "author": "Damir Grgic <damir@tuta.io> (https://github.com/thewebartisan7)",
8
- "main": "src",
8
+ "main": "src/index.js",
9
9
  "types": "src/index.d.ts",
10
10
  "files": [
11
11
  "src"
@@ -39,8 +39,8 @@
39
39
  "style-to-object": "^1.0.6"
40
40
  },
41
41
  "devDependencies": {
42
- "@biomejs/biome": "2.3.2",
43
- "@vitest/coverage-v8": "^3.0.4",
42
+ "@biomejs/biome": "2.3.11",
43
+ "@vitest/coverage-v8": "3.2.4",
44
44
  "conventional-changelog-cli": "^5.0.0",
45
45
  "highlight.js": "^11.6.0",
46
46
  "markdown-it-anchor": "^9.0.1",
@@ -49,6 +49,6 @@
49
49
  "posthtml-beautify": "^0.7.0",
50
50
  "posthtml-include": "^2.0.1",
51
51
  "posthtml-markdownit": "^3.1.0",
52
- "vitest": "^3.0.4"
52
+ "vitest": "3.2.4"
53
53
  }
54
54
  }
package/src/index.d.ts CHANGED
@@ -154,7 +154,7 @@ export type PostHTMLComponents = {
154
154
  *
155
155
  * @default []
156
156
  */
157
- plugins?: Array<() => void>;
157
+ plugins?: Array<() => void>|Record<'before','after', Array<() => void>>;
158
158
 
159
159
  /**
160
160
  * Array of objects used to match tags.
package/src/index.js CHANGED
@@ -140,10 +140,21 @@ module.exports = (options = {}) => tree => {
140
140
  options.props = {...options.expressions.locals};
141
141
  options.aware = {};
142
142
 
143
+ if (Array.isArray(options.plugins)) {
144
+ options.plugins = {
145
+ before: [],
146
+ after: options.plugins
147
+ };
148
+ }
149
+
143
150
  const pushedContent = {};
144
151
 
145
152
  log('Start of processing..', 'init', 'success');
146
153
 
154
+ if (options.plugins.before.length > 0) {
155
+ tree = applyPluginsToTree(tree, options.plugins.before);
156
+ }
157
+
147
158
  return processStacks(
148
159
  processPushes(
149
160
  processTree(options)(
@@ -169,8 +180,8 @@ function processTree(options) {
169
180
  return (tree, messages) => {
170
181
  log(`Processing tree number ${processCounter}..`, 'processTree');
171
182
 
172
- if (options.plugins.length > 0) {
173
- tree = applyPluginsToTree(tree, options.plugins);
183
+ if (options.plugins.after.length > 0) {
184
+ tree = applyPluginsToTree(tree, options.plugins.after);
174
185
  }
175
186
 
176
187
  match.call(tree, options.matcher, currentNode => {
@@ -217,10 +228,15 @@ function processTree(options) {
217
228
  options.expressions.locals = attributes;
218
229
  options.expressions.locals.$slots = filledSlots;
219
230
  // const plugins = [...options.plugins, expressions(options.expressions)];
231
+
232
+ if (options.plugins.before.length > 0) {
233
+ nextNode = applyPluginsToTree(nextNode, options.plugins.before);
234
+ }
235
+
220
236
  nextNode = expressions(options.expressions)(nextNode);
221
237
 
222
- if (options.plugins.length > 0) {
223
- nextNode = applyPluginsToTree(nextNode, options.plugins);
238
+ if (options.plugins.after.length > 0) {
239
+ nextNode = applyPluginsToTree(nextNode, options.plugins.after);
224
240
  }
225
241
 
226
242
  // Process <yield> tag