Switch to JSON frontmatter; no error handling yet

This commit is contained in:
Ludwig Schubert
2017-08-29 11:34:06 -07:00
parent 3b427052b8
commit 3a62ccb1ba
3 changed files with 42 additions and 53 deletions
+20 -11
View File
@@ -2,20 +2,29 @@
<head>
<meta charset="utf8">
<script src="../dist/template.v2.js"></script>
<d-front-matter>
<script type="text/yml">
title: Demo Title Attention and Augmented Recurrent Neural Networks
published: Jan 10, 2017
authors:
- Chris Olah: http://shancarter.com
- Shan Carter: http://shancarter.com
affiliations:
- Google Brain
- Google Brain: http://g.co/brain
</script>
<script id='distill-front-matter' type="text/json">{
"title": "Demo Title Attention and Augmented Recurrent Neural Networks",
"published": "Jan 10, 2017",
"authors": [
{
"author":"Chris Olah",
"authorURL":"https://colah.github.io/",
"affiliation":"Google Brain",
"affiliationURL":"https://g.co/brain"
},
{
"author":"Shan Carter",
"authorURL":"https://shancarter.com/",
"affiliation":"Google Brain",
"affiliationURL":"https://g.co/brain"
}
]
}</script>
</d-front-matter>
<script src="../dist/template.v2.js"></script>
</head>
<body>
+9 -5
View File
@@ -1,15 +1,19 @@
import ymlParse from 'js-yaml';
// import ymlParse from 'js-yaml';
export function parseFrontmatter(element) {
const scriptTag = element.querySelector('script');
if (scriptTag) {
const yml = scriptTag.textContent;
const data = ymlParse.safeLoad(yml);
return data;
const type = scriptTag.getAttribute('type');
if (type.split('/')[1] == 'json') {
const content = scriptTag.textContent;
return JSON.parse(content);
} else {
console.error('Distill only supoprts JSON frontmatter tags anymore; no more YAML.');
}
} else {
console.error('You added a frontmatter tag but did not provide a script tag with front matter data in it. Please take a look at our templates.');
return {};
}
return {};
}
export class FrontMatter extends HTMLElement {
+13 -37
View File
@@ -15,11 +15,18 @@ const RFC = function(date) {
class Author {
constructor(name='', personalURL='', affiliation='', affiliationURL='') {
this.name = name; // 'Chris Olah'
this.personalURL = personalURL; // 'https://colah.github.io'
this.affiliation = affiliation; // 'Google Brain'
this.affiliationURL = affiliationURL; // 'https://g.co/brain'
// constructor(name='', personalURL='', affiliation='', affiliationURL='') {
// this.name = name; // 'Chris Olah'
// this.personalURL = personalURL; // 'https://colah.github.io'
// this.affiliation = affiliation; // 'Google Brain'
// this.affiliationURL = affiliationURL; // 'https://g.co/brain'
// }
constructor(object) {
this.name = object.author; // 'Chris Olah'
this.personalURL = object.authorURL; // 'https://colah.github.io'
this.affiliation = object.affiliation; // 'Google Brain'
this.affiliationURL = object.affiliationURL; // 'https://g.co/brain'
}
// 'Chris'
@@ -113,38 +120,7 @@ export class FrontMatter {
this.title = data.title;
this.publishedDate = new Date(data.published);
this.description = data.description;
const zipped = data.authors.map( (author, index) => [author, data.affiliations[index]]);
this.authors = zipped.map( ([authorEntry, affiliationEntry]) => {
const author = new Author();
// try to get name and personal url
switch (typeof authorEntry) {
case 'object':
author.name = Object.keys(authorEntry)[0];
author.personalURL = authorEntry[author.name];
break;
case 'string':
author.name = authorEntry;
break;
default:
throw new Error('Invalid type in frontmatter author field: ' + authorEntry);
}
// try to get affiliation name and affiliation url
switch (typeof affiliationEntry) {
case 'object':
author.affiliation = Object.keys(affiliationEntry)[0];
author.affiliationURL = affiliationEntry[author.affiliation];
break;
case 'string':
author.affiliation = affiliationEntry;
break;
default:
throw new Error('Invalid type in frontmatter affiliation field: ' + affiliationEntry);
}
return author;
});
this.authors = data.authors.map( (authorObject) => new Author(authorObject));
}
//