-
Notifications
You must be signed in to change notification settings - Fork 11
API
- renderHTML
- renderHTMLToFile
- withBoilerplate
- document.findElementByType
- document.findElementByClassName
- document.findElementById
- document.setTitle
- document.addElementToType
- document.addElementToClass
- document.addElementToId
- document.addElement
- document.addElementToTarget
Param | Type | Description |
---|---|---|
config | object |
{ excludeHTML?: boolean; htmlTagAttributes?: { [key: string]: string }; disablePrettier?: boolean; } |
Returns: STRING
Returns the rendered HTML output as a string.
Returns: PROMISE
Renders the HTML to a given destination file.
const path = require('path');
const htmlCreator = require('html-creator');
const html = new htmlCreator();
html.renderHTMLToFile(
path.join(process.cwd(), 'index.html'),
{
htmlTagAttributes:
{
lang: "EN",
}
}
);
Sets the content to a generic boilerplate for easier setup. If content is passed as a parameter, it will be placed under the BODY tag.
Example:
const html = new htmlCreator([{ type: 'div', content: 'hello there' }]).withBoilerplate();
console.log(html.renderHTML());
// <!DOCTYPE html><html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /></head><body><div>hello there</div></body></html>
Param | Type | Description |
---|---|---|
type | string |
HTML type to search |
Returns: ARRAY
Searches the content for element objects of a given type and returns the results. This is useful for manipulating data after defining the initial content structure.
Param | Type | Description |
---|---|---|
class | string |
class name to search |
Returns: ARRAY
Searches the content for element objects of a given class name and returns the results. This is useful for manipulating data after defining the initial content structure.
For examples of responses see document.findElementByType
Param | Type | Description |
---|---|---|
id | string |
ID to search |
Returns: ARRAY
Searches the content for element objects of a given ID and returns the results. This is useful for manipulating data after defining the initial content structure.
For examples of responses see document.findElementByType
Param | Type | Description |
---|---|---|
title | string |
The title of the document |
Returns: STRING
A helper function to set the title of the document. It searches the content for an existing title tag and replaces it if it exists, otherwise it will be automatically added.
Param | Type | Description |
---|---|---|
type | string |
HTML type to add the element data to |
elementData |
object array
|
A single element object or an array of elements objects |
Chainable. Adds one or several elements to the specified HTML type. The element data will be added to all elements of matching type.
const htmlCreator = require('html-creator');
const html = new htmlCreator([{ type: 'body' }]);
html.addElementToType('body', { type: 'div', content: 'Hooray' });
console.log(html.renderHTML());
// <!DOCTYPE html><body><div>Hooray</div></body></html>
Param | Type | Description |
---|---|---|
className | string |
The name of the class to add the element data to |
elementData |
object array
|
A single element object or an array of elements objects |
Chainable. Adds one or several elements to the specified class. The element data will be added to all elements of matching class name.
const htmlCreator = require('html-creator');
const html = new htmlCreator([{
type: 'body',
content: [{ type: 'div', attributes: { class: 'button' }, content: 'Hello there' }],
}]);
html.addElementToClass('button', { type: 'div', content: 'Hooray' });
console.log(html.renderHTML());
// <!DOCTYPE html><body><div class="button">Hello there<div>Hooray</div></div></body></html>
Param | Type | Description |
---|---|---|
id | string |
The ID of the element to add the new element data to |
elementData |
object array
|
A single element object or an array of elements objects |
Chainable. Adds one or several elements to the element with the given ID.
const htmlCreator = require('html-creator');
const html = new htmlCreator([{
type: 'body',
content: [{ type: 'div', attributes: { id: 'header' } }],
}]);
html.addElementToId('header', { type: 'div', content: 'Wow' });
console.log(html.renderHTML());
// <!DOCTYPE html><body><div id="header"><div>Wow</div></div></body></html>
Param | Type | Description |
---|---|---|
elementData |
object array
|
A single element object or an array of elements objects |
Chainable. Adds an element directly to the bottom of the content.
const htmlCreator = require('html-creator');
const html = new htmlCreator()
.addElement({ type: 'head' })
.addElement({ type: 'body' });
console.log(html.renderHTML());
// <!DOCTYPE html><head></head><body></body></html>
Param | Type | Description |
---|---|---|
elementData |
object array
|
A single element object or an array of elements objects |
target | object |
{ id: 'unique' } { class: 'button' } { type: 'body' }
|
Chainable. Adds an element to the content of a specified target. If you specify a target class or type that exists in multiple places, the new element will be added to all of the elements of the specified type/class.
const htmlCreator = require('html-creator');
const html = new htmlCreator([
{
type: 'body',
content: [{ type: 'div', attributes: { id: 'anID' }, content: 'hello there' }],
},
]);
html.addElementToTarget({ type: 'span', content: 'Yay!' }, { id: 'anID' });
console.log(html.renderHTML());
// <!DOCTYPE html><body><div id="anId">Hello there<span>Yay!</span></div></body></html>