Skip to content

Commit 9d414ab

Browse files
authored
Merge pull request #26 from cossssmin/custom-tag
feat: custom extends tag
2 parents d10ca99 + 9f83a22 commit 9d414ab

3 files changed

Lines changed: 77 additions & 10 deletions

File tree

README.md

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,16 @@ The final HTML will be:
9292

9393
### encoding
9494

95-
The encoding of the parent template. Default: "utf8".
95+
Type: `string`\
96+
Default: `utf8`
97+
98+
The encoding of the parent template.
9699

97100
### plugins
98101

102+
Type: `array`\
103+
Default: `[]`
104+
99105
You can include [other PostHTML plugins](http://posthtml.github.io/posthtml-plugins/) in your templates.
100106
Here is an example of using [posthtml-expressions](https://github.com/posthtml/posthtml-expressions), which allows to use variables and conditions:
101107

@@ -136,17 +142,23 @@ The final HTML will be:
136142

137143
### root
138144

139-
The path to the root template directory. Default: "./".
145+
Type: `string`\
146+
Default: `./`
147+
148+
The path to the root template directory.
140149

141150
### strict
142151

143-
Whether the plugin should disallow undeclared block names. Default: true.
152+
Type: `boolean`\
153+
Default: `true`
154+
155+
Whether the plugin should disallow undeclared block names.
144156

145-
By default, posthtml-extend raises an exception if an undeclared block name is encountered. This can be useful for troubleshooting (i.e. detecting typos in block names), but
157+
By default, the plugin raises an exception if an undeclared block name is encountered. This can be useful for troubleshooting (i.e. detecting typos in block names), but
146158
there are cases where "forward declaring" a block name as an extension point for downstream templates is useful, so this restriction can be lifted by setting the `strict`
147159
option to a false value:
148160

149-
```javascript
161+
```js
150162
const extend = require('posthtml-extend');
151163

152164
const root = './src/html';
@@ -157,7 +169,10 @@ posthtml([extends(options)]).then(result => console.log(result.html));
157169

158170
### slot/fill
159171

160-
Tag names used to match a block with content with a block for inserting content. Default `<block>`
172+
Type: `string`\
173+
Default: `block`
174+
175+
Tag names used to match a content block with a block for inserting content.
161176

162177
`base.html`
163178
```xml
@@ -187,9 +202,7 @@ var html = `<extends src="base.html">
187202
posthtml([require('posthtml-extend')({
188203
slotTagName: 'slot',
189204
fillTagName: 'fill'
190-
})]).process(html).then(function (result) {
191-
console.log(result.html);
192-
});
205+
})]).process(html).then(result => console.log(result.html));
193206
```
194207

195208
The final HTML will be:
@@ -206,3 +219,36 @@ The final HTML will be:
206219
</html>
207220
```
208221

222+
### tagName
223+
224+
Type: `string`\
225+
Default: `extends`
226+
227+
The tag name to use when extending.
228+
229+
```js
230+
var posthtml = require('posthtml');
231+
var html = `<layout src="base.html">
232+
<block name="title">Using a custom tag name</block>
233+
<block name="content">Read the documentation</block>
234+
</layout>`;
235+
236+
posthtml([require('posthtml-extend')({
237+
tagName: 'layout',
238+
})]).process(html).then(result => console.log(result.html));
239+
```
240+
241+
The final HTML will be:
242+
243+
```html
244+
<html>
245+
<head>
246+
<title>Using a custom tag name</title>
247+
</head>
248+
249+
<body>
250+
<div class="content">Read the documentation</div>
251+
<footer>footer content</footer>
252+
</body>
253+
</html>
254+
```

lib/extend.es6

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export default (options = {}) => {
1818
options.strict = Object.prototype.hasOwnProperty.call(options, 'strict') ? !!options.strict : true;
1919
options.slotTagName = options.slotTagName || 'block';
2020
options.fillTagName = options.fillTagName || 'block';
21+
options.tagName = options.tagName || 'extends';
2122

2223
tree = handleExtendsNodes(tree, options, tree.messages);
2324

@@ -34,7 +35,7 @@ export default (options = {}) => {
3435
};
3536

3637
function handleExtendsNodes(tree, options, messages) {
37-
match.call(applyPluginsToTree(tree, options.plugins), {tag: 'extends'}, extendsNode => {
38+
match.call(applyPluginsToTree(tree, options.plugins), {tag: options.tagName}, extendsNode => {
3839
if (! extendsNode.attrs || ! extendsNode.attrs.src) {
3940
throw getError(errors.EXTENDS_NO_SRC);
4041
}

test/extend.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,26 @@ describe('Extend', () => {
117117
});
118118
});
119119

120+
it('should extend layout using a custom tag name', () => {
121+
mfs.writeFileSync('./layout.html', `
122+
<head><block name="head">head</block></head>
123+
<body><block name="body">body</block></body>
124+
`);
125+
126+
return init(
127+
`<layout src="layout.html">
128+
<block name="head"><title>hello world!</title></block>
129+
<block name="body">Some body content</block>
130+
</layout>`,
131+
{
132+
tagName: 'layout'
133+
}).then(html => {
134+
expect(html).toBe(cleanHtml(`
135+
<head><title>hello world!</title></head>
136+
<body>Some body content</body>
137+
`));
138+
});
139+
});
120140

121141
it('should extend inherited layout', () => {
122142
mfs.writeFileSync('./base.html', `

0 commit comments

Comments
 (0)