als-layout 0.1.0 → 0.2.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/layout.js +28 -27
- package/package.json +1 -1
- package/readme.md +16 -11
package/layout.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
class Layout {
|
|
2
|
-
constructor({title,body,description,image,lang='en',url,scripts=
|
|
1
|
+
module.exports = class Layout {
|
|
2
|
+
constructor({title,body,description,image,lang='en',url,twiterName,scripts={},links={},favicon,footer='',header='',charset='UTF-8'}) {
|
|
3
3
|
this.title = title
|
|
4
4
|
this.body = body
|
|
5
5
|
this.description = description
|
|
@@ -11,13 +11,13 @@ class Layout {
|
|
|
11
11
|
this.links = links
|
|
12
12
|
this.footer = footer
|
|
13
13
|
this.header = header
|
|
14
|
-
this.v = v
|
|
15
14
|
this.charset = charset
|
|
15
|
+
this.twiterName = twiterName
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
get({title,body,description,image,lang,url,favicon,footer,header,
|
|
19
|
-
|
|
20
|
-
|
|
18
|
+
get({title,body,description,image,lang,url,favicon,footer='',header='',scripts={},links={},charset=this.charset}) {
|
|
19
|
+
this.scripts = scripts
|
|
20
|
+
this.links = links
|
|
21
21
|
if(title) this.title = title
|
|
22
22
|
if(body) this.body = body
|
|
23
23
|
if(description) this.description = description
|
|
@@ -25,32 +25,28 @@ class Layout {
|
|
|
25
25
|
if(lang) this.lang = lang
|
|
26
26
|
if(url) this.url = url
|
|
27
27
|
if(favicon) this.favicon = favicon
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
let {footerScripts,headerScripts} = this.scripts()
|
|
28
|
+
this.footer = footer
|
|
29
|
+
this.header = header
|
|
30
|
+
let {footerScripts,headerScripts} = this._scripts()
|
|
32
31
|
return /*html*/`<!DOCTYPE html>
|
|
33
|
-
<html lang="${lang}">
|
|
32
|
+
<html lang="${this.lang}">
|
|
34
33
|
<head>
|
|
35
34
|
<meta charset="${charset}">
|
|
36
|
-
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
|
37
|
-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
38
|
-
${favicon ? /*html*/`<link rel="icon" href="${favicon}" type="image/x-icon" >` : ''}
|
|
39
35
|
${this._meta()}
|
|
40
|
-
${this.
|
|
41
|
-
<title>${title}</title>
|
|
36
|
+
${this.favicon ? /*html*/`<link rel="icon" href="${this.favicon}" type="image/x-icon" >` : ''}
|
|
37
|
+
<title>${this.title}</title>
|
|
42
38
|
</head>
|
|
43
39
|
<body>
|
|
44
|
-
${
|
|
40
|
+
${this._header(headerScripts)}
|
|
41
|
+
${this.body}
|
|
45
42
|
${this._footer(footerScripts)}
|
|
46
43
|
</body>
|
|
47
44
|
</html>`
|
|
48
45
|
}
|
|
49
46
|
|
|
50
47
|
_meta() {
|
|
51
|
-
let {title,url,description,image,twiterName
|
|
48
|
+
let {title,url,description,image,twiterName} = this
|
|
52
49
|
let meta = [
|
|
53
|
-
{charset},
|
|
54
50
|
{'http-equiv':"X-UA-Compatible", content:"IE=edge"},
|
|
55
51
|
{name:"viewport",content:"width=device-width, initial-scale=1.0"},
|
|
56
52
|
{property:"og:type", content:"website"},
|
|
@@ -68,23 +64,26 @@ class Layout {
|
|
|
68
64
|
meta.push({name:"twitter:card", content:'article'})
|
|
69
65
|
}
|
|
70
66
|
if(twiterName) meta.push({name:"twitter:site",content:twiterName})
|
|
71
|
-
if(title) meta.push({property:"og:title", content:
|
|
67
|
+
if(title) meta.push({property:"og:title", content:title})
|
|
72
68
|
if(url) meta.push({property:"og:url",content:url})
|
|
69
|
+
return meta.map(obj=>
|
|
70
|
+
/*html*/`<meta ${Object.keys(obj).map(propName => `${propName}="${obj[propName]}"`).join('')}>`).join('')
|
|
73
71
|
}
|
|
74
72
|
|
|
75
73
|
_footer(scripts) {
|
|
76
|
-
return /*html*/`${
|
|
74
|
+
return /*html*/`${scripts} ${this.footer}`
|
|
77
75
|
}
|
|
78
76
|
|
|
79
77
|
_header(scripts) {
|
|
80
|
-
return /*html*/`${this._styles()} ${
|
|
78
|
+
return /*html*/`${this._styles()} ${scripts} ${this.header}`
|
|
81
79
|
}
|
|
82
80
|
|
|
83
81
|
_scripts() {
|
|
84
82
|
let footerScripts = ''
|
|
85
83
|
let headerScripts = ''
|
|
86
|
-
this.scripts.forEach(
|
|
87
|
-
let
|
|
84
|
+
Object.keys(this.scripts).forEach(name => {
|
|
85
|
+
let script = this.scripts[name]
|
|
86
|
+
let {inner='',src='',async,crossorigin,defer,integrity,referrerpolicy,type,footer=false,v} = script
|
|
88
87
|
v = v ? '?'+v : ''
|
|
89
88
|
async = async ? ' async ' : ''
|
|
90
89
|
defer = defer ? ' defer ' : ''
|
|
@@ -92,7 +91,8 @@ class Layout {
|
|
|
92
91
|
integrity = integrity ? ` integrity="${integrity}" ` : ''
|
|
93
92
|
referrerpolicy = referrerpolicy ? ` referrerpolicy="${referrerpolicy}" ` : ''
|
|
94
93
|
type = type ? ` type="${type}" ` : ''
|
|
95
|
-
|
|
94
|
+
src = src ? `src="${src}${v}"`:''
|
|
95
|
+
script = /*html*/` <script ${src}${referrerpolicy}${type}${integrity}${crossorigin}${defer}${async}>${inner}</script> `
|
|
96
96
|
if(footer) footerScripts += script
|
|
97
97
|
else headerScripts += script
|
|
98
98
|
})
|
|
@@ -100,8 +100,9 @@ class Layout {
|
|
|
100
100
|
}
|
|
101
101
|
|
|
102
102
|
_styles() {
|
|
103
|
-
return this.links.map(
|
|
104
|
-
let
|
|
103
|
+
return Object.keys(this.links).map(name => {
|
|
104
|
+
let link = this.links[name]
|
|
105
|
+
let {href,rel='stylesheet',crossorigin,hreflang,media,referrerpolicy,sizes,type,v} = link
|
|
105
106
|
v = v ? '?'+v : ''
|
|
106
107
|
crossorigin = crossorigin ? ` crossorigin="${crossorigin}" ` : ''
|
|
107
108
|
referrerpolicy = referrerpolicy ? ` referrerpolicy="${referrerpolicy}" ` : ''
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -7,14 +7,19 @@
|
|
|
7
7
|
let Layout = require('als-layout')
|
|
8
8
|
// Create Layout object
|
|
9
9
|
let layout = new Layout({
|
|
10
|
-
title,body,description,image,
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
title,body,description,image,
|
|
11
|
+
lang='en',url,twiterName,
|
|
12
|
+
scripts={},links={},
|
|
13
|
+
favicon,footer='',header='',
|
|
14
|
+
charset='UTF-8'
|
|
13
15
|
})
|
|
14
16
|
// Get updated layout
|
|
15
17
|
let pageLayout = layout.get({
|
|
16
|
-
|
|
17
|
-
|
|
18
|
+
title,body,description,image,
|
|
19
|
+
lang,url,favicon,
|
|
20
|
+
footer='',header='',
|
|
21
|
+
scripts={},links={},
|
|
22
|
+
charset=this.charset
|
|
18
23
|
})
|
|
19
24
|
```
|
|
20
25
|
|
|
@@ -22,10 +27,10 @@ let pageLayout = layout.get({
|
|
|
22
27
|
## scripts and links
|
|
23
28
|
|
|
24
29
|
```javascript
|
|
25
|
-
let scripts =
|
|
26
|
-
{inner='',src='',async,crossorigin,defer,integrity,referrerpolicy,type,footer=false,v
|
|
27
|
-
|
|
28
|
-
let links =
|
|
29
|
-
{href,rel='stylesheet',crossorigin,hreflang,media,referrerpolicy,sizes,type,v
|
|
30
|
-
|
|
30
|
+
let scripts = {
|
|
31
|
+
scriptName:{inner='',src='',async,crossorigin,defer,integrity,referrerpolicy,type,footer=false,v}
|
|
32
|
+
}
|
|
33
|
+
let links = {
|
|
34
|
+
linkName:{href,rel='stylesheet',crossorigin,hreflang,media,referrerpolicy,sizes,type,v}
|
|
35
|
+
}
|
|
31
36
|
```
|