From a67799a670d3f1220400f7ad8acbeb168edddc2d Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Wed, 5 Jan 2022 14:08:21 -0800 Subject: [PATCH] Allow markdown readme above the gallery pages (#11103) --- build-scripts/gulp/gallery.js | 38 ++++++++++++++++++++---- gallery/src/demos/demo-ha-alert.markdown | 14 +++++++++ gallery/src/ha-gallery.ts | 26 ++++++++++++++-- 3 files changed, 71 insertions(+), 7 deletions(-) create mode 100644 gallery/src/demos/demo-ha-alert.markdown diff --git a/build-scripts/gulp/gallery.js b/build-scripts/gulp/gallery.js index 9522d48b5c..16848ec723 100644 --- a/build-scripts/gulp/gallery.js +++ b/build-scripts/gulp/gallery.js @@ -2,6 +2,7 @@ const gulp = require("gulp"); const fs = require("fs"); const path = require("path"); +const marked = require("marked"); const env = require("../env"); const paths = require("../paths"); @@ -20,19 +21,46 @@ gulp.task("gather-gallery-demos", async function gatherDemos() { path.resolve(paths.gallery_dir, "src/demos") ); + const galleryBuild = path.resolve(paths.gallery_dir, "build"); + fs.mkdirSync(galleryBuild, { recursive: true }); + let content = "export const DEMOS = {\n"; for (const file of files) { + if (!file.endsWith(".ts")) { + continue; + } const demoId = path.basename(file, ".ts"); - const demoPath = "../src/demos/" + demoId; - content += ` "${demoId.substring(5)}": () => import("${demoPath}"),\n`; + const descriptionFile = path.resolve( + paths.gallery_dir, + "src/demos", + `${demoId}.markdown` + ); + const hasDescription = fs.existsSync(descriptionFile); + if (hasDescription) { + const descriptionContent = fs.readFileSync(descriptionFile, "utf-8"); + fs.writeFileSync( + path.resolve(galleryBuild, `${demoId}-description.ts`), + ` + import {html} from "lit"; + export default html\`${marked(descriptionContent)}\` + ` + ); + } + const demoPath = `../src/demos/${demoId}`; + const descriptionPath = `./${demoId}-description`; + content += ` "${demoId.substring(5)}": { + ${ + hasDescription + ? `description: () => import("${descriptionPath}").then(m => m.default),` + : "" + } + load: () => import("${demoPath}") + },\n`; } content += "};"; - const galleryBuild = path.resolve(paths.gallery_dir, "build"); - - fs.mkdirSync(galleryBuild, { recursive: true }); fs.writeFileSync( path.resolve(galleryBuild, "import-demos.ts"), content, diff --git a/gallery/src/demos/demo-ha-alert.markdown b/gallery/src/demos/demo-ha-alert.markdown new file mode 100644 index 0000000000..0f5a01d43b --- /dev/null +++ b/gallery/src/demos/demo-ha-alert.markdown @@ -0,0 +1,14 @@ +The `` element is used to display a message to the user that requires attention. + +The `header` option should not be used without a description. + + +Usage: + +```html +Look, an alert! +``` + +This will show: + +Look, an alert! diff --git a/gallery/src/ha-gallery.ts b/gallery/src/ha-gallery.ts index d7d38b2381..4f044096cb 100644 --- a/gallery/src/ha-gallery.ts +++ b/gallery/src/ha-gallery.ts @@ -3,6 +3,7 @@ import "@material/mwc-drawer"; import "@material/mwc-top-app-bar-fixed"; import { html, css, LitElement, PropertyValues } from "lit"; import { customElement, property, query } from "lit/decorators"; +import { until } from "lit/directives/until"; import "../../src/components/ha-icon-button"; import "../../src/managers/notification-manager"; import { haStyle } from "../../src/resources/styles"; @@ -99,7 +100,23 @@ class HaGallery extends LitElement {
${this._demo}
-
${dynamicElement(`demo-${this._demo}`)}
+
+ ${DEMOS[this._demo].description + ? html` + ${until( + DEMOS[this._demo].description().then( + (content) => html` + +
${content}
+
+ ` + ), + "" + )} + ` + : ""} + ${dynamicElement(`demo-${this._demo}`)} +