als-layout 0.1.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.
Files changed (3) hide show
  1. package/layout.js +115 -0
  2. package/package.json +9 -0
  3. package/readme.md +31 -0
package/layout.js ADDED
@@ -0,0 +1,115 @@
1
+ class Layout {
2
+ constructor({title,body,description,image,lang='en',url,scripts=[],links=[],favicon,footer,header,v,charset='UTF-8'}) {
3
+ this.title = title
4
+ this.body = body
5
+ this.description = description
6
+ this.image = image
7
+ this.lang = lang
8
+ this.url = url
9
+ this.favicon = favicon
10
+ this.scripts = scripts
11
+ this.links = links
12
+ this.footer = footer
13
+ this.header = header
14
+ this.v = v
15
+ this.charset = charset
16
+ }
17
+
18
+ get({title,body,description,image,lang,url,favicon,footer,header,v,scripts,links,charset=this.charset}) {
19
+ if(scripts) this.scripts = [...this.scripts,scripts]
20
+ if(links) this.links = [...this.links,links]
21
+ if(title) this.title = title
22
+ if(body) this.body = body
23
+ if(description) this.description = description
24
+ if(image) this.image = image
25
+ if(lang) this.lang = lang
26
+ if(url) this.url = url
27
+ if(favicon) this.favicon = favicon
28
+ if(footer) this.footer = footer
29
+ if(header) this.header = header
30
+ if(v) this.v = v
31
+ let {footerScripts,headerScripts} = this.scripts()
32
+ return /*html*/`<!DOCTYPE html>
33
+ <html lang="${lang}">
34
+ <head>
35
+ <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
+ ${this._meta()}
40
+ ${this._header(headerScripts)}
41
+ <title>${title}</title>
42
+ </head>
43
+ <body>
44
+ ${body}
45
+ ${this._footer(footerScripts)}
46
+ </body>
47
+ </html>`
48
+ }
49
+
50
+ _meta() {
51
+ let {title,url,description,image,twiterName,charset} = this
52
+ let meta = [
53
+ {charset},
54
+ {'http-equiv':"X-UA-Compatible", content:"IE=edge"},
55
+ {name:"viewport",content:"width=device-width, initial-scale=1.0"},
56
+ {property:"og:type", content:"website"},
57
+ ]
58
+ if(description) {
59
+ meta.push({name:"description",content:description})
60
+ meta.push({property:"og:description",content:description})
61
+ meta.push({name:"twitter:description",content:description})
62
+ }
63
+ if(image) {
64
+ meta.push({property:"og:image", content:image})
65
+ meta.push({name:"twitter:image", content:image})
66
+ meta.push({name:"twitter:card", content:'summary_large_image'})
67
+ } else {
68
+ meta.push({name:"twitter:card", content:'article'})
69
+ }
70
+ if(twiterName) meta.push({name:"twitter:site",content:twiterName})
71
+ if(title) meta.push({property:"og:title", content:"title"})
72
+ if(url) meta.push({property:"og:url",content:url})
73
+ }
74
+
75
+ _footer(scripts) {
76
+ return /*html*/`${this.footer} ${scripts}`
77
+ }
78
+
79
+ _header(scripts) {
80
+ return /*html*/`${this._styles()} ${this.header} ${scripts}`
81
+ }
82
+
83
+ _scripts() {
84
+ let footerScripts = ''
85
+ let headerScripts = ''
86
+ this.scripts.forEach(script => {
87
+ let {inner='',src='',async,crossorigin,defer,integrity,referrerpolicy,type,footer=false,v=this.v} = script
88
+ v = v ? '?'+v : ''
89
+ async = async ? ' async ' : ''
90
+ defer = defer ? ' defer ' : ''
91
+ crossorigin = crossorigin ? ` crossorigin="${crossorigin}" ` : ''
92
+ integrity = integrity ? ` integrity="${integrity}" ` : ''
93
+ referrerpolicy = referrerpolicy ? ` referrerpolicy="${referrerpolicy}" ` : ''
94
+ type = type ? ` type="${type}" ` : ''
95
+ let script = /*html*/` <script src="${src}${v}"${referrerpolicy}${type}${integrity}${crossorigin}${defer}${async}>${inner}</script> `
96
+ if(footer) footerScripts += script
97
+ else headerScripts += script
98
+ })
99
+ return {footerScripts,headerScripts}
100
+ }
101
+
102
+ _styles() {
103
+ return this.links.map(link => {
104
+ let {href,rel='stylesheet',crossorigin,hreflang,media,referrerpolicy,sizes,type,v=this.v} = link
105
+ v = v ? '?'+v : ''
106
+ crossorigin = crossorigin ? ` crossorigin="${crossorigin}" ` : ''
107
+ referrerpolicy = referrerpolicy ? ` referrerpolicy="${referrerpolicy}" ` : ''
108
+ type = type ? ` type="${type}" ` : ''
109
+ hreflang = hreflang ? ` hreflang="${hreflang}" ` : ''
110
+ media = media ? ` media="${media}" ` : ''
111
+ sizes = sizes ? ` media="${sizes}" ` : ''
112
+ return /*html*/`<link href="${href}${v}" rel="${rel}" ${crossorigin}${referrerpolicy}${type}${hreflang}${media}${sizes}></link>`
113
+ }).join(' ')
114
+ }
115
+ }
package/package.json ADDED
@@ -0,0 +1,9 @@
1
+ {
2
+ "name": "als-layout",
3
+ "version": "0.1.0",
4
+ "description": "Being tested. create html layout with node",
5
+ "main": "layout.js",
6
+ "keywords": [],
7
+ "author": "",
8
+ "license": "ISC"
9
+ }
package/readme.md ADDED
@@ -0,0 +1,31 @@
1
+ # Als-layout
2
+
3
+ **being tested**
4
+
5
+ ## Syntax
6
+ ```javascript
7
+ let Layout = require('als-layout')
8
+ // Create Layout object
9
+ let layout = new Layout({
10
+ title,body,description,image,lang='en',url,
11
+ scripts=[],links=[],
12
+ favicon,footer,header,v,charset
13
+ })
14
+ // Get updated layout
15
+ let pageLayout = layout.get({
16
+ scripts,links, // added to existing scripts and links
17
+ title,body,description,image,lang,url,favicon,footer,header,v
18
+ })
19
+ ```
20
+
21
+
22
+ ## scripts and links
23
+
24
+ ```javascript
25
+ let scripts = [
26
+ {inner='',src='',async,crossorigin,defer,integrity,referrerpolicy,type,footer=false,v=this.v}
27
+ ]
28
+ let links = [
29
+ {href,rel='stylesheet',crossorigin,hreflang,media,referrerpolicy,sizes,type,v=this.v}
30
+ ]
31
+ ```