Skip to content

Commit

Permalink
fix: use parser to render about page content
Browse files Browse the repository at this point in the history
  • Loading branch information
chosww committed Jan 6, 2025
1 parent a275d04 commit 958b0f4
Show file tree
Hide file tree
Showing 10 changed files with 304 additions and 29 deletions.
3 changes: 3 additions & 0 deletions eleventy.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { EleventyRenderPlugin } from "@11ty/eleventy";
import eleventyNavigationPlugin from "@11ty/eleventy-navigation";
import fluidPlugin from "eleventy-plugin-fluid";
import footnotesPlugin from "eleventy-plugin-footnotes";
import parse from "./src/assets/scripts/parse.js";

export default function eleventy(eleventyConfig) {
eleventyConfig.addPlugin(eleventyNavigationPlugin);
Expand Down Expand Up @@ -29,6 +30,8 @@ export default function eleventy(eleventyConfig) {
});
});

eleventyConfig.addTransform("parse", parse);

eleventyConfig.addPassthroughCopy({
"src/admin/config.yml": "admin/config.yml"
});
Expand Down
207 changes: 207 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"cross-env": "^7.0.3",
"debug": "^4.3.5",
"husky": "^9.0.11",
"linkedom": "^0.18.6",
"lint-staged": "^15.2.7",
"markdownlint-cli2": "^0.15.0",
"markdownlint-config-fluid": "^0.1.5",
Expand Down
7 changes: 1 addition & 6 deletions src/_includes/layouts/about.njk
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
{% extends "layouts/base.njk" %}

{% block content %}
{% for key, value in sections %}
<section>
<h2>{{ key }}</h2>
<p>{{ value }}</p>
</section>
{% endfor %}
{{ content | safe }}
{% endblock %}
9 changes: 4 additions & 5 deletions src/_includes/layouts/base.njk
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@
{% include "partials/global/scripts.njk" %}
</head>
<body>
<main>
{% include "partials/global/header.njk" %}
{% include "partials/components/navigation.njk" %}
{% from "partials/components/card.macro.njk" import card %}
{% if hasBanner %}
<div class="banner bg-{{ bannerBgColor }}">
<div class="banner__text">
<div class="banner__title">
{{ bannerTitle | safe }}
</div>
<h1>{{ bannerTitle | safe }}</h1>
{{ bannerBody }}
</div>
{% if bannerImage %}
Expand All @@ -28,7 +27,6 @@
{% endif %}
{% block content %}{% endblock %}
{% if collections['projects_' + lang] %}
{% set cardImage = "yes" %}
<section class="display">
<div class="display__title">
<h2>Projects</h2>
Expand Down Expand Up @@ -88,6 +86,7 @@
</div>
</section>
{% endif %}
{% include "partials/global/footer.njk" %}
</main>
{% include "partials/global/footer.njk" %}
</body>
</html>
3 changes: 3 additions & 0 deletions src/_includes/partials/global/scripts.njk
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
{% uioScripts %}
<script type="text/javascript" src="/assets/scripts/app.js" defer></script>
{% if page.fileSlug === "about" %}
<script type="text/javascript" src="/assets/scripts/parse.js" defer></script>
{% endif %}
36 changes: 36 additions & 0 deletions src/assets/scripts/parse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { parseHTML } from "linkedom";

export default (value, outputPath) => {
if (outputPath && outputPath.includes(".html")) {
const { document } = parseHTML(value);
if (document) {
const main = document.getElementsByTagName("main");
const updatedMain = document.createElement("main");
let section = document.createElement("section");

if (main[0]?.children) {
for (const element of main[0].children) {
if (element.tagName === "H2" && element.classList?.length === 0) {
if (section.children.length > 0) {
updatedMain.append(section);
section = document.createElement("section");
}
section.append(element);
} else if (element.tagName === "P" && element.classList?.length === 0) {
section.append(element);
} else {
if (section.children.length > 0) {
updatedMain.append(section);
}
updatedMain.append(element);
}
}
main[0].replaceWith(updatedMain);
}

return "<!DOCTYPE html>\r\n" + document.documentElement?.outerHTML;
}
}

return value;
};
Loading

0 comments on commit 958b0f4

Please sign in to comment.