From 348eceb11002f40ed0a86ef1d7f77265afe6d40d Mon Sep 17 00:00:00 2001 From: milahu Date: Mon, 15 Mar 2021 21:22:32 +0100 Subject: [PATCH] add demo: custom-elements --- examples/custom-elements/.eleventy.js | 82 +++++++++++++++++++++++++++ examples/custom-elements/index.md | 27 +++++++++ examples/custom-elements/package.json | 15 +++++ 3 files changed, 124 insertions(+) create mode 100644 examples/custom-elements/.eleventy.js create mode 100644 examples/custom-elements/index.md create mode 100644 examples/custom-elements/package.json diff --git a/examples/custom-elements/.eleventy.js b/examples/custom-elements/.eleventy.js new file mode 100644 index 0000000..31084f0 --- /dev/null +++ b/examples/custom-elements/.eleventy.js @@ -0,0 +1,82 @@ +const transformDomPlugin = require('eleventy-plugin-transformdom'); + + + +// generate transformers for transformDomPlugin +function getElementTransformer(selector, options = {}) { + + const defaultOptions = { + name: 'div', + attributes: (e => Object.fromEntries(Array.from(e.attributes).map(a => [a.name, a.value]))), + class: (e => e.localName), // lowercase tag name + }; + options = Object.assign(defaultOptions, options); + + const getName = ( + (typeof options.name == 'function') ? (e => options.name(e)) : + (() => options.name) + ); + + const getAttributes = ( + (typeof options.attributes == 'function') ? (e => options.attributes(e)) : + (typeof options.attributes == 'object') ? (() => options.attributes) : + (() => false) + ); + + const getExtraClass = ( + (typeof options.class == 'function') ? (e => options.class(e)) : + (typeof options.class == 'string') ? (() => options.class) : + (() => false) + ); + + const domTransformer = { + selector, + transform: ({ elements, document }) => { + for (const element of elements) { + const nameNew = getName(element); + if (!nameNew) continue; // no replace + const attributesNew = getAttributes(element) || {}; + const extraClassNew = getExtraClass(element); + + const elementNew = document.createElement(nameNew); + + // copy attributes + for (const [name, value] of Object.entries(attributesNew)) { + elementNew.setAttribute(name, value); + } + + // copy content + elementNew.innerHTML = element.innerHTML; + + // add class + if (extraClassNew) elementNew.classList.add(extraClassNew); + + element.replaceWith(elementNew); + } + }, + }; + return domTransformer; +} + + + +module.exports = function (eleventyConfig) { + const domTransformers = []; + + // in: hello + // out:
hello
+ domTransformers.push(getElementTransformer('page', { class: 'page-element' })); + + // in: dont wrap me + // out: dont wrap me + domTransformers.push(getElementTransformer('nw', { name: 'span', class: 'nowrap-element' })); + + // in: good dayguten tag + // out: good dayguten tag + for (const langKey of ['en', 'de']) { + domTransformers.push(getElementTransformer(langKey, + { name: 'span', attributes: (e => ({ lang: e.localName })), class: false })); + } + + eleventyConfig.addPlugin(transformDomPlugin, domTransformers); +}; diff --git a/examples/custom-elements/index.md b/examples/custom-elements/index.md new file mode 100644 index 0000000..e47a595 --- /dev/null +++ b/examples/custom-elements/index.md @@ -0,0 +1,27 @@ +# Custom elements demo + +This is a demo of +[eleventy-plugin-transformdom](https://github.com/liamfiddler/eleventy-plugin-transformdom) +which transforms custom elements. + +result: + + + dont wrap me +

+ good day + guten tag +

+
+ +expected source: + + diff --git a/examples/custom-elements/package.json b/examples/custom-elements/package.json new file mode 100644 index 0000000..579ad39 --- /dev/null +++ b/examples/custom-elements/package.json @@ -0,0 +1,15 @@ +{ + "name": "eleventy-plugin-transformdom-examples-custom-elements", + "version": "1.0.0", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1", + "start": "eleventy --serve", + "build": "eleventy" + }, + "author": "@liamfiddler", + "license": "MIT", + "devDependencies": { + "@11ty/eleventy": "^0.11.0", + "eleventy-plugin-transformdom": "../../" + } +}