Skip to content

Commit

Permalink
Adding static custom event
Browse files Browse the repository at this point in the history
  • Loading branch information
tnylea committed Sep 29, 2023
1 parent ee1d4f5 commit 3f9f015
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@devdojo/static",
"version": "1.0.15",
"version": "1.0.18",
"description": "Static Site Generator",
"main": "index.js",
"scripts": {
Expand Down
5 changes: 4 additions & 1 deletion src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,15 @@ module.exports = {
let contentAttributes = fm(content).attributes;

let staticJS = "window.toc = JSON.parse('" + JSON.stringify(tableOfContents.json).replace(/'/g, "\\'") + "'); window.frontmatter=JSON.parse('" + JSON.stringify(contentAttributes).replace(/'/g, "\\'") + "'); localStorage.setItem('toc', '" + JSON.stringify(tableOfContents.json).replace(/'/g, "\\'") + "'); localStorage.setItem('frontmatter', '" + JSON.stringify(contentAttributes).replace(/'/g, "\\'") + "');";
let attrTags = "<script>document.addEventListener('DOMContentLoaded', (event) => { " + staticJS + " }); " + staticJS + "</script>";
let staticJSEvent = "window.dispatchEvent(new CustomEvent('static:content', { detail: { frontmatter: '" + JSON.stringify(tableOfContents.json).replace(/'/g, "\\'") + "', toc: '" + JSON.stringify(tableOfContents.json).replace(/'/g, "\\'") + "' } }));";
let attrTags = "<script>document.addEventListener('DOMContentLoaded', (event) => { " + staticJS + " }); " + staticJSEvent + "</script>";

// process frontmatter conditions
page = this.processFrontMatterConditions(page, contentAttributes);
page = this.processFrontMatterReplacements(page, contentAttributes);



if(page.includes('[static_js]')){
page = page.replace('[static_js]', attrTags);
} else {
Expand Down

0 comments on commit 3f9f015

Please sign in to comment.