als-layout 1.0.0 → 1.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/lib/build-root.js CHANGED
@@ -1,8 +1,8 @@
1
- const { buildFromCache, Root, parseHTML } = require('als-document')
1
+ const { buildFromCache, Root, parseHTML, cacheDoc } = require('als-document')
2
2
 
3
3
  function buildRoot(item) {
4
4
  const root =
5
- (item instanceof Root) ? item
5
+ (item instanceof Root) ? buildFromCache(cacheDoc(item))
6
6
  : (typeof item === 'string') ? parseHTML(item)
7
7
  : (typeof item === 'object') ? buildFromCache(item)
8
8
  : new Root()
@@ -1,5 +1,7 @@
1
1
  const addMeta = require('./add-meta')
2
- function addImage(image,layout) {
2
+ function addImage(image,version, layout) {
3
+ if (image && version) image += src.includes('?') ? '&' : '?' + `v=${version}`
4
+
3
5
  addMeta({property:'og:image',content:image},layout)
4
6
  addMeta({name:'twitter:image',content:image},layout)
5
7
  addMeta({name:'twitter:card',content:'summary_large_image'},layout)
@@ -1,7 +1,8 @@
1
1
  const { SingleNode } = require('als-document')
2
- function addLink(href, layout) {
2
+ function addLink(href, version, layout) {
3
3
  let linkElement = layout.root.$(`link[rel=stylesheet][href="${href}"]`)
4
4
  if (linkElement) return
5
+ if (href && version) href += href.includes('?') ? '&' : '?' + `v=${version}`
5
6
  linkElement = new SingleNode('link', { rel: 'stylesheet', href })
6
7
  layout.head.insert(2, linkElement)
7
8
  return layout
@@ -1,9 +1,11 @@
1
1
  const { Node } = require('als-document')
2
- function addScript(attributes={}, innerHTML, head = true, layout) {
3
- if (attributes.src && layout.root.$(`script[src="${attributes.src}"]`)) return
4
- const script = new Node('script', attributes)
2
+ function addScript(attrs = {}, innerHTML = '', head = true, version, layout) {
3
+ const { src } = attrs
4
+ if (src && layout.root.$(`script[src="${attrs.src}"]`)) return
5
+ if (src && version) attrs.src += src.includes('?') ? '&' : '?' + `v=${version}`
6
+ const script = new Node('script', attrs)
5
7
  script.innerHTML = innerHTML
6
- if(head) layout.root.head.insert(2, script)
8
+ if (head) layout.root.head.insert(2, script)
7
9
  else layout.body.insert(3, script)
8
10
  return layout
9
11
  }
@@ -1,12 +1,16 @@
1
1
  const addMeta = require('./add-meta')
2
- const {SingleNode} = require('als-document')
2
+ const { SingleNode } = require('als-document')
3
3
 
4
- function getUrl(url,host,layout) {
5
- url = new URL(url,host).href.replace(/\/$/,'')
6
- addMeta({property:'og:url',content:url},layout)
7
- const canonicalElement = layout.root.$('link[rel="canonical"]')
8
- if(canonicalElement) canonicalElement.setAttribute('href',url)
9
- else layout.head.insert(2,new SingleNode('link',{rel:'canonical',href:url}))
4
+ function getUrl(url, host, layout) {
5
+ try {
6
+ url = new URL(url, host).href.replace(/\/$/, '')
7
+ addMeta({ 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(`url ${url} with host ${host} is not valid url`)
13
+ }
10
14
  return layout
11
15
  }
12
16
 
package/lib/layout.js CHANGED
@@ -34,15 +34,16 @@ class Layout {
34
34
  get cached() { return cacheDoc(this.root) }
35
35
  get clone() { return new Layout(this.root, this.options) }
36
36
 
37
+ version(v) {this.v = v; return this;}
37
38
  keywords(keywords = []) { return addKeywords(keywords, this) }
38
39
  description(description) { return addDescription(description, this) }
39
40
  title(title) { return addTitle(title, this) }
40
41
  style(styles, minified) { return addStyle(styles, minified, this) }
41
- image(image) { return addImage(image, this) }
42
+ image(image,v=this.v) { return addImage(image, v, this) }
42
43
  url(url, host = this.options.host) { return addUrl(url, host, this) }
43
44
  favicon(href) { return addFavicon(href, this) }
44
- script(attributes, innerHTML, head = true) { return addScript(attributes, innerHTML, head, this) }
45
- link(href) { return addLink(href, this) }
45
+ script(attributes, innerHTML, head = true, v=this.v) { return addScript(attributes, innerHTML, head, v, this) }
46
+ link(href, v=this.v) { return addLink(href, v, this) }
46
47
  charset(newCharset = 'UTF-8') { return charset(newCharset, this) }
47
48
  viewport(newViewport = 'width=device-width, initial-scale=1.0') { return viewport(newViewport, this) }
48
49
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "als-layout",
3
- "version": "1.0.0",
3
+ "version": "1.2.0",
4
4
  "description": "Html layout constructor",
5
5
  "main": "index.js",
6
6
  "directories": {
@@ -14,7 +14,7 @@
14
14
  "license": "ISC",
15
15
  "dependencies": {
16
16
  "als-css-parser": "^0.5.0",
17
- "als-document": "^1.0.1-beta",
17
+ "als-document": "^1.0.1",
18
18
  "als-simple-css": "^8.0.0"
19
19
  }
20
20
  }
package/readme.md CHANGED
@@ -2,6 +2,11 @@
2
2
 
3
3
  Html layout constructor.
4
4
 
5
+ ## Change log
6
+ * version method
7
+ * script,link,image - version parameter
8
+ * updated als-document version
9
+
5
10
  ## install
6
11
 
7
12
  ```bash
@@ -18,13 +23,14 @@ const layout = new Layout()
18
23
  .title('Test title') // adding/updating title and meta[og:title]
19
24
  .favicon('/favicon.png') // adding/updating link[rel=icon][type=image/x-icon] with new href
20
25
  .keywords(['some','keyword']) // adding/updating meta[name=keywords]. not adding existing keywords
21
- .image('/main-image.png') // adding/updating meta - og:image,twitter:image,twitter:card
26
+ .image('/main-image.png','1.5') // adding/updating meta - og:image,twitter:image,twitter:card
22
27
  .description('Cool site') // adding/updating meta og:description,twitter:description and description tag
28
+ .version('1.0.0') // adds version parameter to link, script.src and image
23
29
  .url('/some', 'http://site.com') // adding/updating meta[og:url] and link[rel="canonical"]
24
30
  .style([{body:{m:0,bgc:'whitesmoke'}}]) // adding as simple-css styles to existing/new style tag
25
31
  .style('body {margin:0; backgroung-color:whitesmoke;}',true) // adding css styles to existing/new style tag. Second parameter is minified (default=false).
26
- .link('/styles.css') // adding link[rel=stylesheet] if such href not exists
27
- .script({src:'/app.js'},'', true) // set script with src to head if such src not exists
32
+ .link('/styles.css','2.0') // adding link[rel=stylesheet] if such href not exists
33
+ .script({src:'/app.js'},'', true,'3.0') // set script with src to head if such src not exists
28
34
  .script({}, 'console.log("hello world")', false) // set script with script code to footer
29
35
 
30
36
 
@@ -37,6 +43,7 @@ layout.cached // cached DOM
37
43
  layout.clone // new layout object clone for curent object
38
44
  ```
39
45
 
46
+ In image, script.src and link, last parameter is version which using 'layout.v' (defined by layout.version(v)) if not defined.
40
47
 
41
48
  ## Advanced usage
42
49
 
@@ -15,7 +15,7 @@ describe('Basic tests', function () {
15
15
 
16
16
  it('should create an instance of Root from ready root', () => {
17
17
  const root = new Root()
18
- assert(buildRoot(root) === root)
18
+ assert(buildRoot(root) !== root)
19
19
  });
20
20
 
21
21
  it('should add !DOCTYPE', () => {