mirror of
https://github.com/wassname/template.git
synced 2026-06-29 13:33:34 +08:00
ffb5eaf3f7
Update render.js script; expose it as bin when used as npm dependency.
43 lines
1.3 KiB
JavaScript
43 lines
1.3 KiB
JavaScript
export function parseFrontmatter(element) {
|
|
const scriptTag = element.querySelector('script');
|
|
if (scriptTag) {
|
|
const type = scriptTag.getAttribute('type');
|
|
if (type.split('/')[1] == 'json') {
|
|
const content = scriptTag.textContent;
|
|
return JSON.parse(content);
|
|
} else {
|
|
console.error('Distill only supports 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 {};
|
|
}
|
|
|
|
export class FrontMatter extends HTMLElement {
|
|
|
|
static get is() { return 'd-front-matter'; }
|
|
|
|
constructor() {
|
|
super();
|
|
|
|
const options = {childList: true, characterData: true, subtree: true};
|
|
const observer = new MutationObserver( (entries) => {
|
|
for (const entry of entries) {
|
|
if (entry.target.nodeName === 'SCRIPT' || entry.type === 'characterData') {
|
|
const data = parseFrontmatter(this);
|
|
this.notify(data);
|
|
}
|
|
}
|
|
});
|
|
observer.observe(this, options);
|
|
}
|
|
|
|
notify(data) {
|
|
const options = { detail: data, bubbles: true };
|
|
const event = new CustomEvent('onFrontMatterChanged', options);
|
|
document.dispatchEvent(event);
|
|
}
|
|
|
|
}
|