forked from mainmatter/mainmatter.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add mechanism for arbitrary head tags (mainmatter#659)
* dedicated class for writing head tags * render head tags in SSR build * add static meta tags * add og:title meta tag * add canonical url tag * fix object iteration * add article published time meta tag * render meta date for blog posts * format * fix prerender build
- Loading branch information
Showing
17 changed files
with
170 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
lib/generate-blog-components/lib/files/**/* | ||
lib/generate-blog/lib/templates/**/* | ||
lib/global-css/css/vendor/normalize.css | ||
src/ui/components/Header/template.hbs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import Component from '@glimmer/component'; | ||
|
||
export default class HeadTag extends Component { | ||
constructor(options) { | ||
super(options); | ||
|
||
this.setMetaTags(); | ||
} | ||
|
||
public willDestroy() { | ||
this.headTags.remove(this.args.name, this.args.keys); | ||
} | ||
|
||
private setMetaTags() { | ||
this.headTags.write(this.args.name, this.args.keys, this.args.values, this.args.content); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
:scope { | ||
block-name: HeadTag; | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
export default class HeadTags { | ||
|
||
|
||
public static create(): HeadTags { | ||
return new HeadTags(); | ||
} | ||
private document: HTMLDocument = window.document; | ||
|
||
public write(tagName, keyAttrs = {}, contentAttrs = {}, textContent = null) { | ||
let element = this.getElement(tagName, keyAttrs); | ||
|
||
let attrs = { | ||
...keyAttrs, | ||
...contentAttrs | ||
}; | ||
for (let attr of Object.keys(attrs)) { | ||
element.setAttribute(attr, attrs[attr]); | ||
} | ||
if (textContent) { | ||
element.textContent = textContent; | ||
} | ||
|
||
this.document.head.appendChild(element); | ||
} | ||
|
||
public remove(tagName, keyAttrs = {}) { | ||
let element = this.getElement(tagName, keyAttrs); | ||
|
||
this.document.head.removeChild(element); | ||
} | ||
|
||
private getElement(tagName, keyAttrs): Element { | ||
let selector = buildSelector(tagName, keyAttrs); | ||
let element = this.document.querySelector(selector); | ||
if (!element) { | ||
element = this.document.createElement(tagName); | ||
} | ||
return element; | ||
} | ||
} | ||
|
||
function buildSelector(tagName, attrs): string { | ||
let selector = `head > ${tagName}`; | ||
if (Object.keys(attrs).length > 0) { | ||
let attrSelectors = Object.keys(attrs).map((attr) => `[${attr}="${attrs[attr]}"]`); | ||
selector = `${selector}${attrSelectors.join('')}`; | ||
} | ||
return selector; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default function(params, named) { | ||
return named; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// tslint:disable-next-line:no-var-requires | ||
const SimpleDOM = require('simple-dom'); | ||
|
||
export default class SSRHeadTags { | ||
private document: SimpleDOM.Document; | ||
|
||
constructor(options) { | ||
this.document = options.document; | ||
} | ||
|
||
public static create(options): SSRHeadTags { | ||
return new SSRHeadTags(options); | ||
} | ||
|
||
public write(tagName, keyAttrs = {}, contentAttrs = {}, textContent = null) { | ||
let element = this.document.createElement(tagName); | ||
|
||
let attrs = { | ||
...keyAttrs, | ||
...contentAttrs | ||
}; | ||
for (let attr in attrs) { | ||
element.setAttribute(attr, attrs[attr]); | ||
} | ||
|
||
if (textContent) { | ||
let text = this.document.createTextNode(textContent); | ||
element.appendChild(text); | ||
} | ||
|
||
this.document.head.appendChild(element); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters