als-layout 6.1.0 → 7.0.1
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/build.js +8 -0
- package/index.mjs +83 -0
- package/layout.js +127 -0
- package/package.json +14 -8
- package/readme.md +101 -198
- package/src/layout.js +85 -0
- package/tests/constructor.test.js +2 -2
- package/tests/description.test.js +1 -1
- package/tests/favicon.test.js +1 -1
- package/tests/image.test.js +1 -1
- package/tests/integrative.test.js +1 -1
- package/tests/keywords.test.js +1 -1
- package/tests/link.test.js +4 -14
- package/tests/script.test.js +1 -2
- package/tests/style.test.js +2 -9
- package/tests/url.test.js +3 -11
- package/tests/viewport.test.js +1 -1
- package/docs/#.md +0 -19
- package/docs/0-change-log.md +0 -10
- package/docs/1-basic-usage.md +0 -37
- package/docs/2-cloning.md +0 -35
- package/docs/3-advanced-usage.md +0 -48
- package/docs/4-res-and-status.md +0 -10
- package/docs/5-api.md +0 -97
- package/index.js +0 -3
- package/lib/elements/index.js +0 -38
- package/lib/elements/keywords.js +0 -19
- package/lib/elements/link.js +0 -13
- package/lib/elements/meta.js +0 -12
- package/lib/elements/script.js +0 -25
- package/lib/elements/style.js +0 -20
- package/lib/elements/url.js +0 -18
- package/lib/layout.js +0 -52
- package/scripts/build-readme.js +0 -8
package/docs/#.md
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
# als-layout Documentation
|
|
2
|
-
|
|
3
|
-
`als-layout` is a JavaScript library designed to simplify and enhance the process of constructing and managing web page layouts. It provides a comprehensive API for modifying HTML documents dynamically, allowing developers to add, update, and manipulate various elements such as meta tags, styles, scripts, and more.
|
|
4
|
-
|
|
5
|
-
The `als-layout` library is versatile, suitable for:
|
|
6
|
-
- Building dynamic web pages that require frequent updates to their metadata, styles, or scripts.
|
|
7
|
-
- Creating templating systems where multiple page layouts share similar structures but differ in content or styling.
|
|
8
|
-
- Developing web applications that need to dynamically adjust their UI based on user interactions or data changes.
|
|
9
|
-
|
|
10
|
-
## Installation and Adding
|
|
11
|
-
To use the `als-layout` library in your project, you can install it via npm and then include it in your JavaScript files:
|
|
12
|
-
|
|
13
|
-
```bash
|
|
14
|
-
npm i als-layout
|
|
15
|
-
```
|
|
16
|
-
|
|
17
|
-
```js
|
|
18
|
-
const Layout = require('als-layout')
|
|
19
|
-
```
|
package/docs/0-change-log.md
DELETED
package/docs/1-basic-usage.md
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
## Basic Usage
|
|
2
|
-
|
|
3
|
-
### Initialization
|
|
4
|
-
To start using `als-layout`, you first need to create a new instance of `Layout`. This instance will serve as the foundation for building and modifying your web page.
|
|
5
|
-
|
|
6
|
-
```js
|
|
7
|
-
const Layout = require('als-layout');
|
|
8
|
-
const layout = new Layout();
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
### Adding Different Elements
|
|
12
|
-
Once you have your `Layout` instance, you can easily add or modify various elements of your web page. Here are some examples of how you can use the library to customize your layout:
|
|
13
|
-
|
|
14
|
-
```js
|
|
15
|
-
const layout = new Layout()
|
|
16
|
-
.viewport() // default width=device-width, initial-scale=1.0
|
|
17
|
-
.title('Test title') // adding/updating title and meta[og:title]
|
|
18
|
-
.favicon('/favicon.png') // adding/updating link[rel=icon][type=image/x-icon] with new href
|
|
19
|
-
.keywords(['some', 'keyword']) // adding/updating meta[name=keywords]. not adding existing keywords
|
|
20
|
-
.image('/main-image.png', '1.5') // adding/updating meta - og:image, twitter:image, twitter:card
|
|
21
|
-
.description('Cool site') // adding/updating meta og:description, twitter:description, and description tag
|
|
22
|
-
.url('/some', 'http://site.com') // adding/updating meta[og:url] and link[rel="canonical"]
|
|
23
|
-
.style('body {margin:0; background-color:whitesmoke;}', true) // adding css styles to existing/new style tag. Second parameter is minified (default=false).
|
|
24
|
-
.link('/styles.css') // adding link[rel=stylesheet] if such href not exists
|
|
25
|
-
.script({src:'/app.js'}, '', true) // set script with src to head if such src not exists
|
|
26
|
-
.script({}, 'console.log("hello world")', false) // set script with script code to footer
|
|
27
|
-
|
|
28
|
-
// Accessors for document parts
|
|
29
|
-
layout.body // getter for body element (if not exists, created)
|
|
30
|
-
layout.head // getter for head element (if not exists, created)
|
|
31
|
-
layout.html // getter for html Element (if not exists, created)
|
|
32
|
-
|
|
33
|
-
// Outputs
|
|
34
|
-
layout.rawHtml // raw HTML of the document
|
|
35
|
-
layout.clone // creates a new layout object clone for current object
|
|
36
|
-
```
|
|
37
|
-
|
package/docs/2-cloning.md
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
## Cloning Functionality
|
|
2
|
-
|
|
3
|
-
### What is Cloning and Why is it Necessary?
|
|
4
|
-
Cloning in the `als-layout` library refers to creating a complete, independent copy of the existing `Layout` instance. This functionality is crucial when you need to generate multiple pages or versions of a page from a single base layout without affecting the original setup.
|
|
5
|
-
|
|
6
|
-
### How to Use Cloning
|
|
7
|
-
To clone a `Layout` instance, simply use the `clone` method. This method creates a new `Layout` instance with the same properties and settings as the original, allowing for independent modifications without interference.
|
|
8
|
-
|
|
9
|
-
```js
|
|
10
|
-
const newLayout = layout.clone;
|
|
11
|
-
```
|
|
12
|
-
|
|
13
|
-
### Benefits of Cloning
|
|
14
|
-
- **Efficiency:** Cloning is highly efficient, especially for creating pages with similar structures but different content or styles. It avoids the overhead of reinitializing and reconfiguring a new `Layout` instance from scratch.
|
|
15
|
-
- **Speed:** Cloning is fast, typically taking less than 20ms even for large pages. This makes it ideal for high-performance web applications that need to dynamically generate content.
|
|
16
|
-
- **Isolation:** Changes made to a cloned `Layout` do not affect the original, ensuring that each instance can be modified independently based on specific requirements.
|
|
17
|
-
|
|
18
|
-
Cloning is particularly useful in scenarios where templates or base layouts are used repeatedly with slight variations, providing a robust and scalable solution for web page generation.
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
### propsToClone
|
|
22
|
-
|
|
23
|
-
You can add properties to add when cloning the object, by adding name of properties to `Layout.propsToClone` which is array.
|
|
24
|
-
|
|
25
|
-
Example:
|
|
26
|
-
|
|
27
|
-
```js
|
|
28
|
-
Layout.propsToClone.push('projectName')
|
|
29
|
-
|
|
30
|
-
const layout = new Layout()
|
|
31
|
-
layout.projectName = 'Cool project'
|
|
32
|
-
|
|
33
|
-
const cloned = layout.clone
|
|
34
|
-
console.log(cloned.projectName) // 'Cool project'
|
|
35
|
-
```
|
package/docs/3-advanced-usage.md
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
## Advanced Usage
|
|
2
|
-
|
|
3
|
-
The `als-layout` library allows for sophisticated manipulation of web page layouts, providing robust tools for creating dynamic and complex web pages. Below is an advanced example demonstrating various capabilities of the library:
|
|
4
|
-
|
|
5
|
-
```js
|
|
6
|
-
const Layout = require('als-layout')
|
|
7
|
-
|
|
8
|
-
// Starting with a basic HTML template and specifying the host for URL methods
|
|
9
|
-
const raw = /*html*/`<html></html>`
|
|
10
|
-
const host = 'http://example.com';
|
|
11
|
-
const options = {
|
|
12
|
-
logger,
|
|
13
|
-
host,
|
|
14
|
-
minified=false
|
|
15
|
-
}
|
|
16
|
-
const layout = new Layout(raw, options).lang('fr')
|
|
17
|
-
console.log(layout.rawHtml)
|
|
18
|
-
// <!DOCTYPE html><html lang="fr"><head></p></head><body></body></html>
|
|
19
|
-
|
|
20
|
-
// Cloning the initial layout to create a specialized page
|
|
21
|
-
const homePage = layout.clone
|
|
22
|
-
homeAutoReload = layout.clone
|
|
23
|
-
homePage.title('Home page')
|
|
24
|
-
homePage.body.innerHTML = /*html*/`<h1>Home page</h1>`
|
|
25
|
-
console.log(homePage.rawHtml)
|
|
26
|
-
// <!DOCTYPE html><html lang="fr"><head><title>Home page</title><meta property="og:title" content="Home page"></head><body><h1>Home page</h1></body></html>
|
|
27
|
-
|
|
28
|
-
// Adding script that reloads the page every minute
|
|
29
|
-
homeAutoReload.script({}, 'setTimeout(function() { window.location.reload(); }, 60000);', false)
|
|
30
|
-
console.log(homeAutoReload.rawHtml)
|
|
31
|
-
// <!DOCTYPE html><html lang="fr"><head><title>Automatic Reload Page</title><meta property="og:title" content="Automatic Reload Page"></head><body><script>setTimeout(function() { window.location.reload(); }, 60000);</script></body></html>
|
|
32
|
-
|
|
33
|
-
// Demonstrating dynamic stylesheet linkage with versioning
|
|
34
|
-
homePage.link('/css/main.css')
|
|
35
|
-
console.log(homePage.rawHtml)
|
|
36
|
-
// Includes link to the stylesheet with version parameter to ensure fresh cache
|
|
37
|
-
```
|
|
38
|
-
|
|
39
|
-
In this example:
|
|
40
|
-
|
|
41
|
-
- We start with a basic HTML template and use the `lang` method to set the language.
|
|
42
|
-
- We use the `clone` method to create two versions of the base layout: one for the home page and another that automatically reloads every minute.
|
|
43
|
-
- We manipulate the `body` of the `homePage` to include custom HTML.
|
|
44
|
-
- We add a script to `homeAutoReload` that sets up an automatic page reload, showcasing how to insert JavaScript dynamically.
|
|
45
|
-
- We dynamically add a versioned link to a stylesheet in the `homePage`, demonstrating control over caching and resource management.
|
|
46
|
-
|
|
47
|
-
This advanced example illustrates how `als-layout` can be used to handle complex scenarios and requirements in web development, enhancing the flexibility and power at your disposal.
|
|
48
|
-
|
package/docs/4-res-and-status.md
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
## Response with status
|
|
2
|
-
|
|
3
|
-
In version 6, added two methods:
|
|
4
|
-
1. `status(statusCode)` - adds the statusCode to `__status` property
|
|
5
|
-
2. `end(res)`
|
|
6
|
-
1. writes head with `__status, { 'Content-Type': 'text/html' }`
|
|
7
|
-
2. runs `res.end(this.rawHtml)`
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
The main idea, is to add quick way to response with layout.
|
package/docs/5-api.md
DELETED
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
## API
|
|
2
|
-
|
|
3
|
-
### Constructor
|
|
4
|
-
|
|
5
|
-
#### `new Layout(html: string, options: object)`
|
|
6
|
-
|
|
7
|
-
Creates a new `Layout` instance.
|
|
8
|
-
|
|
9
|
-
- **html**: The initial HTML document string.
|
|
10
|
-
- **options**: Configuration options for the layout instance.
|
|
11
|
-
- **minified**: Boolean (default: `false`). Determines if inline CSS and JavaScript should be minified.
|
|
12
|
-
- **logger**: Function (default: `console`). Function for errors in minifiying and url method.
|
|
13
|
-
- **host**: String (defult:`undefined`). String for url method.
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
### Properties
|
|
17
|
-
|
|
18
|
-
#### `layout.rawHtml`
|
|
19
|
-
|
|
20
|
-
Returns the inner HTML of the document.
|
|
21
|
-
|
|
22
|
-
#### `layout.clone`
|
|
23
|
-
|
|
24
|
-
Creates a clone of the current layout instance, preserving `options`.
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
#### `__status`
|
|
28
|
-
|
|
29
|
-
Stores the statusCode for `end` method.
|
|
30
|
-
|
|
31
|
-
### Methods
|
|
32
|
-
|
|
33
|
-
#### `lang(lang: string): this`
|
|
34
|
-
|
|
35
|
-
Sets the `lang` attribute on the `<html>` element.
|
|
36
|
-
|
|
37
|
-
#### `title(title: string): this`
|
|
38
|
-
|
|
39
|
-
Sets the document title and creates an Open Graph title meta tag.
|
|
40
|
-
|
|
41
|
-
#### `description(description: string): this`
|
|
42
|
-
|
|
43
|
-
Adds description meta tags for SEO and social platforms.
|
|
44
|
-
|
|
45
|
-
#### `favicon(href: string): this`
|
|
46
|
-
|
|
47
|
-
Sets or updates the favicon URL.
|
|
48
|
-
|
|
49
|
-
#### `meta(props: object): this`
|
|
50
|
-
|
|
51
|
-
Adds or updates a `<meta>` tag with specified attributes.
|
|
52
|
-
|
|
53
|
-
- **props**: An object where each key-value pair corresponds to a meta attribute.
|
|
54
|
-
|
|
55
|
-
#### `keywords(keywords: array): this`
|
|
56
|
-
|
|
57
|
-
Sets or appends keywords in the `content` attribute of a `<meta name="keywords">` tag.
|
|
58
|
-
|
|
59
|
-
#### `viewport(viewport: string): this`
|
|
60
|
-
|
|
61
|
-
Sets the viewport meta tag. Default: `width=device-width, initial-scale=1.0`.
|
|
62
|
-
|
|
63
|
-
#### `image(image: string): this`
|
|
64
|
-
|
|
65
|
-
Sets meta tags for Open Graph and Twitter cards for an image.
|
|
66
|
-
|
|
67
|
-
#### `style(styles: string, minified: boolean = this.options.minified): this`
|
|
68
|
-
|
|
69
|
-
Adds inline CSS to the document. If `minified` is true, the CSS is minified.
|
|
70
|
-
|
|
71
|
-
- **styles**: CSS string.
|
|
72
|
-
- **minified**: Boolean (optional).
|
|
73
|
-
|
|
74
|
-
#### `url(url: string, host: string = this.URL): this`
|
|
75
|
-
|
|
76
|
-
Sets the canonical URL and Open Graph URL meta tags.
|
|
77
|
-
|
|
78
|
-
#### `script(attrs: object = {}, innerHTML: string = '', head: boolean = true, minified: boolean = this.options.minified): this`
|
|
79
|
-
|
|
80
|
-
Adds a `<script>` tag to the document.
|
|
81
|
-
|
|
82
|
-
- **attrs**: Attributes for the `<script>` tag.
|
|
83
|
-
- **innerHTML**: Inline JavaScript (optional).
|
|
84
|
-
- **head**: Boolean (default: `true`). If `false`, the script is added to `<body>`.
|
|
85
|
-
- **minified**: Boolean. If true, inline JavaScript is minified.
|
|
86
|
-
|
|
87
|
-
#### `link(href: string, attributes: object = { rel: "stylesheet", type: "text/css" }): this`
|
|
88
|
-
|
|
89
|
-
Adds a `<link>` tag for external stylesheets.
|
|
90
|
-
|
|
91
|
-
#### `status(statusCode:number): this`
|
|
92
|
-
|
|
93
|
-
Adds the statusCode to `__status` property
|
|
94
|
-
|
|
95
|
-
#### `end(res):undefined`
|
|
96
|
-
|
|
97
|
-
Response with res(rawHtml)
|
package/index.js
DELETED
package/lib/elements/index.js
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
const meta = require('./meta')
|
|
2
|
-
const script = require('./script')
|
|
3
|
-
const style = require('./style')
|
|
4
|
-
const addKeywords = require('./keywords')
|
|
5
|
-
const addLink = require('./link')
|
|
6
|
-
const addUrl = require('./url')
|
|
7
|
-
|
|
8
|
-
const { SingleNode } = require('als-document');
|
|
9
|
-
|
|
10
|
-
function getDescription(description,layout) {
|
|
11
|
-
meta({ name: 'description', content: description },layout)
|
|
12
|
-
meta({ property: 'og:description', content: description },layout)
|
|
13
|
-
meta({ property: 'twitter:description', content: description },layout)
|
|
14
|
-
return layout
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
function getFavicon(href,layout) {
|
|
18
|
-
const faviconElement = layout.root.$('link[rel=icon][type=image/x-icon]')
|
|
19
|
-
if (faviconElement) faviconElement.setAttribute('href', href)
|
|
20
|
-
else layout.head.insert(2, new SingleNode('link', { rel: 'icon', href, type: 'image/x-icon' }))
|
|
21
|
-
return layout
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
function getViewport(viewport = 'width=device-width, initial-scale=1.0',layout) {
|
|
25
|
-
const element = layout.root.$('meta[name="viewport"]')
|
|
26
|
-
if (element) element.setAttribute('content', viewport)
|
|
27
|
-
else layout.head.insert(2, new SingleNode('meta', { name: 'viewport', content: viewport }))
|
|
28
|
-
return layout
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
function getImage(image,layout) {
|
|
32
|
-
meta({ property: 'og:image', content: image },layout)
|
|
33
|
-
meta({ name: 'twitter:image', content: image },layout)
|
|
34
|
-
meta({ name: 'twitter:card', content: 'summary_large_image' },layout)
|
|
35
|
-
return layout
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
module.exports = { meta, getDescription, getFavicon, getViewport, getImage, script, style, addKeywords, addLink, addUrl }
|
package/lib/elements/keywords.js
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
const { SingleNode } = require('als-document');
|
|
2
|
-
|
|
3
|
-
function keywords(keywords = [],layout) {
|
|
4
|
-
let keywordsElement = layout.root.$('meta[name=keywords]')
|
|
5
|
-
if (!keywordsElement) keywordsElement = new SingleNode('meta', { name: 'keywords' })
|
|
6
|
-
const content = keywordsElement.getAttribute('content')
|
|
7
|
-
const existingKeywords = content ? content.split(',') : []
|
|
8
|
-
keywords.forEach(keyword => {
|
|
9
|
-
keyword = keyword.trim()
|
|
10
|
-
if (!existingKeywords.includes(keyword)) existingKeywords.push(keyword)
|
|
11
|
-
});
|
|
12
|
-
if (existingKeywords.length) {
|
|
13
|
-
keywordsElement.setAttribute('content', existingKeywords.join())
|
|
14
|
-
layout.head.insert(2, keywordsElement)
|
|
15
|
-
}
|
|
16
|
-
return layout
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
module.exports = keywords
|
package/lib/elements/link.js
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
const { SingleNode } = require('als-document');
|
|
2
|
-
|
|
3
|
-
function link(href, attributes = { rel: "stylesheet", type: "text/css" },layout) {
|
|
4
|
-
if (!href || typeof href !== 'string') return layout
|
|
5
|
-
const selector = `link[rel=stylesheet][href^="${href}"]`
|
|
6
|
-
let linkElement = layout.root.$(selector)
|
|
7
|
-
if (linkElement) return
|
|
8
|
-
linkElement = new SingleNode('link', { href, ...attributes })
|
|
9
|
-
layout.head.insert(2, linkElement)
|
|
10
|
-
return layout
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
module.exports = link
|
package/lib/elements/meta.js
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
const { SingleNode } = require('als-document');
|
|
2
|
-
|
|
3
|
-
function meta(props,layout) {
|
|
4
|
-
const entries = Object.entries(props)
|
|
5
|
-
const [name, value] = entries[0]
|
|
6
|
-
const selector = `meta[${name}="${value}"]`
|
|
7
|
-
const metaElement = layout.root.$(selector)
|
|
8
|
-
if (metaElement) entries.forEach(([name, v]) => metaElement.setAttribute(name, props[name]))
|
|
9
|
-
else layout.head.insert(2, new SingleNode('meta', props))
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
module.exports = meta
|
package/lib/elements/script.js
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
const UglifyJS = require("uglify-js");
|
|
2
|
-
const { Node } = require('als-document');
|
|
3
|
-
|
|
4
|
-
function script(attrs = {}, innerHTML = '', head = true, minified, layout) {
|
|
5
|
-
if (typeof attrs !== 'object' || attrs === null || Array.isArray(attrs)) attrs = {}
|
|
6
|
-
if (attrs.src) {
|
|
7
|
-
const selector = `script[src="${attrs.src}"]`
|
|
8
|
-
if (attrs.src && layout.root.$(selector)) return layout
|
|
9
|
-
}
|
|
10
|
-
if (Object.keys(attrs).length || innerHTML) {
|
|
11
|
-
const script = new Node('script', attrs)
|
|
12
|
-
if (innerHTML) {
|
|
13
|
-
if (minified) {
|
|
14
|
-
try { innerHTML = UglifyJS.minify(innerHTML).code }
|
|
15
|
-
catch (error) { layout.logger.error(error) }
|
|
16
|
-
}
|
|
17
|
-
script.innerHTML = innerHTML
|
|
18
|
-
}
|
|
19
|
-
if (head) layout.head.insert(2, script)
|
|
20
|
-
else layout.html.insert(2, script)
|
|
21
|
-
}
|
|
22
|
-
return layout
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
module.exports = script
|
package/lib/elements/style.js
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
const uglifycss = require('uglifycss');
|
|
2
|
-
const { Node } = require('als-document');
|
|
3
|
-
|
|
4
|
-
function style(styles, minified, layout) {
|
|
5
|
-
if (typeof styles !== 'string') throw 'styles parameter should be string';
|
|
6
|
-
if (minified) {
|
|
7
|
-
try { styles = uglifycss.processString(styles); }
|
|
8
|
-
catch (error) { layout.logger.error(error) }
|
|
9
|
-
}
|
|
10
|
-
let styleElement = layout.root.$('style')
|
|
11
|
-
if (styleElement) styleElement.innerHTML = styleElement.innerHTML + '\n' + styles
|
|
12
|
-
else {
|
|
13
|
-
styleElement = new Node('style')
|
|
14
|
-
styleElement.innerHTML = styles
|
|
15
|
-
layout.head.insert(2, styleElement)
|
|
16
|
-
}
|
|
17
|
-
return layout
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
module.exports = style
|
package/lib/elements/url.js
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
const meta = require('./meta');
|
|
2
|
-
const { SingleNode } = require('als-document');
|
|
3
|
-
|
|
4
|
-
function addUrl(url, host,layout) {
|
|
5
|
-
try {
|
|
6
|
-
url = (host ? new URL(url, host) : new URL(url)).href.replace(/\/$/, '')
|
|
7
|
-
meta({ property: 'og:url', content: url },layout)
|
|
8
|
-
const canonicalElement = layout.root.$('link[rel="canonical"]')
|
|
9
|
-
if (canonicalElement) canonicalElement.setAttribute('href', url)
|
|
10
|
-
else layout.head.insert(2, new SingleNode('link', { rel: 'canonical', href: url }))
|
|
11
|
-
} catch (error) {
|
|
12
|
-
console.log(error)
|
|
13
|
-
layout.logger.log(`url "${url}" with host "${host}" is not valid url`)
|
|
14
|
-
}
|
|
15
|
-
return layout
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
module.exports = addUrl
|
package/lib/layout.js
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
const { Document } = require('als-document');
|
|
2
|
-
const { meta, getDescription, getFavicon, getViewport, getImage, script, style, addKeywords, addLink, addUrl } = require('./elements/index')
|
|
3
|
-
|
|
4
|
-
class Layout extends Document {
|
|
5
|
-
static propsToClone = [];
|
|
6
|
-
static options = { logger: console, host: '', minified: false }
|
|
7
|
-
|
|
8
|
-
constructor(html, options = {}) {
|
|
9
|
-
options = { ...Layout.options, ...options }
|
|
10
|
-
super(html, options.host);
|
|
11
|
-
this.options = options
|
|
12
|
-
this.logger = options.logger || console
|
|
13
|
-
this.root = this.html
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
get rawHtml() { return this.innerHTML }
|
|
17
|
-
get clone() {
|
|
18
|
-
const clone = new this.constructor(new Document(this, this.URL), this.options)
|
|
19
|
-
Layout.propsToClone.forEach(prop => clone[prop] = this[prop])
|
|
20
|
-
return clone
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
__status = 200;
|
|
24
|
-
status(status) { this.__status = status; return this }
|
|
25
|
-
end(res) {
|
|
26
|
-
res.writeHead(this.__status, { 'Content-Type': 'text/html' })
|
|
27
|
-
res.end(this.rawHtml)
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
lang(lang) { this.html.setAttribute('lang', lang); return this }
|
|
31
|
-
link(href, attributes) { return addLink(href, attributes, this) }
|
|
32
|
-
keywords(keywords = []) { return addKeywords(keywords, this) }
|
|
33
|
-
style(styles, minified = this.options.minified) { return style(styles, minified, this) }
|
|
34
|
-
url(url, host = this.URL) { return addUrl(url, host, this) }
|
|
35
|
-
meta(props) { return meta(props, this) }
|
|
36
|
-
description(description) { return getDescription(description,this) }
|
|
37
|
-
favicon(href) { return getFavicon(href,this) }
|
|
38
|
-
viewport(viewport) { return getViewport(viewport,this) }
|
|
39
|
-
image(image) { return getImage(image,this) }
|
|
40
|
-
script(attrs = {}, innerHTML = '', head = true, minified = this.options.minified) {
|
|
41
|
-
return script(attrs, innerHTML, head, minified, this)
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
title(title) {
|
|
45
|
-
super.title // create title tag if not exists
|
|
46
|
-
super.title = title
|
|
47
|
-
this.meta({ property: 'og:title', content: title })
|
|
48
|
-
return this
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
module.exports = Layout
|
package/scripts/build-readme.js
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
const { readFileSync, writeFileSync, readdirSync } = require('fs')
|
|
2
|
-
const { join } = require('path')
|
|
3
|
-
|
|
4
|
-
const docsDir = join(__dirname, '..','docs')
|
|
5
|
-
const files = readdirSync(docsDir)
|
|
6
|
-
const content = files.map(file => readFileSync(join(docsDir, file), 'utf-8')).join('\n');
|
|
7
|
-
writeFileSync(join(__dirname, '..','readme.md'), content, 'utf-8')
|
|
8
|
-
|