|
| 1 | +const path = require('path'); |
| 2 | + |
| 3 | +function innerMD(content = '') { |
| 4 | + const trimmed = content.trim(); |
| 5 | + return trimmed && `\n\n\n${trimmed}\n\n\n`; |
| 6 | +} |
| 7 | + |
| 8 | +function type(content = '', { lang = 'ts' } = {}) { |
| 9 | + return content?.trim?.() && |
| 10 | + `\n\n\`\`\`${lang}\n${content.trim()}\n\n\`\`\`\n\n`; |
| 11 | +} |
| 12 | + |
| 13 | +// TODO: Object.groupBy instead of reduce |
| 14 | +function undeprecate(acc, x) { |
| 15 | + acc ??= {}; |
| 16 | + acc.deprecated ??= []; |
| 17 | + acc.current ??= []; |
| 18 | + if (x.deprecated) { |
| 19 | + acc.deprecated.push(x); |
| 20 | + } else { |
| 21 | + acc.current.push(x); |
| 22 | + } |
| 23 | + return acc; |
| 24 | +} |
| 25 | + |
| 26 | +/** @param {import('@11ty/eleventy/src/UserConfig')} eleventyConfig */ |
| 27 | +module.exports = function(eleventyConfig) { |
| 28 | + /** |
| 29 | + * docs pages contain a #styling-hooks anchor as back compat for older versions of the page |
| 30 | + * to prevent this id from rendering more than once, we track the number of times each page |
| 31 | + * renders css custom properties. |
| 32 | + */ |
| 33 | + const cssStylingHookIdTracker = new WeakSet(); |
| 34 | + |
| 35 | + async function render(tpl, content, kwargs) { |
| 36 | + return eleventyConfig.javascriptFunctions.renderFile( |
| 37 | + path.join(process.cwd(), '/docs/_plugins/element-docs/templates/', tpl), |
| 38 | + { content, ...kwargs }, |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + /** Rebuild the site in watch mode when the templates for this plugin change */ |
| 43 | + eleventyConfig.addWatchTarget('docs/_plugins/element-docs/templates/*.njk'); |
| 44 | + |
| 45 | + eleventyConfig.addFilter('cemMdHeading', (header, length = 2) => |
| 46 | + innerMD(`${Array.from({ length }, () => '#').join('')} ${header}`)); |
| 47 | + |
| 48 | + eleventyConfig.addFilter('cemInnerMD', innerMD); |
| 49 | + |
| 50 | + eleventyConfig.addFilter('cemType', type); |
| 51 | + |
| 52 | + eleventyConfig.addPairedAsyncShortcode('band', async function(content, kwargs) { |
| 53 | + return render('band.njk', content, kwargs); |
| 54 | + }); |
| 55 | + |
| 56 | + /** |
| 57 | + * Render the overview of a component page |
| 58 | + */ |
| 59 | + eleventyConfig.addPairedAsyncShortcode('renderOverview', async function(content, kwargs = {}) { |
| 60 | + const docsPage = this.ctx._; |
| 61 | + const tagName = docsPage.packageTagName(kwargs); |
| 62 | + const description = docsPage.manifest.getDescription(tagName); |
| 63 | + const header = kwargs.title ?? this.title; |
| 64 | + const { renderTitleInOverview = false } = this.options ?? {}; |
| 65 | + const renderedTitle = |
| 66 | + !renderTitleInOverview ? '' |
| 67 | + : await render('band.njk', '', { level: 1, header }); |
| 68 | + return `${renderedTitle}\n${await render('overview.njk', content, { description, ...kwargs })}`; |
| 69 | + }); |
| 70 | + |
| 71 | + /** |
| 72 | + * Render the list of element attributes |
| 73 | + */ |
| 74 | + eleventyConfig.addPairedAsyncShortcode('renderAttributes', async function(content, kwargs = {}) { |
| 75 | + const docsPage = this.ctx._; |
| 76 | + const tagName = docsPage.packageTagName(kwargs); |
| 77 | + const { deprecated, current: attributes } = docsPage.manifest.getAttributes(tagName).reduce(undeprecate, {}); |
| 78 | + return render('attributes.njk', content, { attributes, deprecated, ...kwargs }); |
| 79 | + }); |
| 80 | + |
| 81 | + /** |
| 82 | + * Render the list of element DOM properties |
| 83 | + * @deprecated: render your own templates. To be removed in version 3 |
| 84 | + */ |
| 85 | + eleventyConfig.addPairedAsyncShortcode('renderProperties', async function(content = '', kwargs = {}) { |
| 86 | + const docsPage = this.ctx._; |
| 87 | + const tagName = docsPage.packageTagName(kwargs); |
| 88 | + const { deprecated, current: properties } = docsPage.manifest.getProperties(tagName).reduce(undeprecate, {}); |
| 89 | + return render('properties.njk', content, { properties, deprecated, ...kwargs }); |
| 90 | + }); |
| 91 | + |
| 92 | + /** |
| 93 | + * Render a table of element CSS Custom Properties |
| 94 | + * @deprecated: render your own templates. To be removed in version 3 |
| 95 | + */ |
| 96 | + eleventyConfig.addPairedAsyncShortcode('renderCssCustomProperties', async function(content, kwargs = {}) { |
| 97 | + const hasStylingHooks = cssStylingHookIdTracker.has(this); |
| 98 | + cssStylingHookIdTracker.add(this); |
| 99 | + const docsPage = this.ctx._; |
| 100 | + const tagName = docsPage.packageTagName(kwargs); |
| 101 | + const { deprecated, current: cssProperties } = docsPage.manifest.getCssCustomProperties(tagName).reduce(undeprecate, {}); |
| 102 | + return render('css-custom-properties.njk', content, { cssProperties, deprecated, hasStylingHooks, ...kwargs }); |
| 103 | + }); |
| 104 | + |
| 105 | + /** |
| 106 | + * Render the list of element CSS Shadow Parts |
| 107 | + * @deprecated: render your own templates. To be removed in version 3 |
| 108 | + */ |
| 109 | + eleventyConfig.addPairedAsyncShortcode('renderCssParts', async function(content, kwargs = {}) { |
| 110 | + const docsPage = this.ctx._; |
| 111 | + const tagName = docsPage.packageTagName(kwargs); |
| 112 | + const { deprecated, current: parts } = docsPage.manifest.getCssParts(tagName).reduce(undeprecate, {}); |
| 113 | + return render('css-shadow-parts.njk', content, { parts, deprecated, ...kwargs }); |
| 114 | + }); |
| 115 | + |
| 116 | + /** |
| 117 | + * Render the list of events for the element |
| 118 | + */ |
| 119 | + eleventyConfig.addPairedAsyncShortcode('renderEvents', async function(content, kwargs = {}) { |
| 120 | + const docsPage = this.ctx._; |
| 121 | + const tagName = docsPage.packageTagName(kwargs); |
| 122 | + const { deprecated, current: events } = docsPage.manifest.getEvents(tagName).reduce(undeprecate, {}); |
| 123 | + return render('events.njk', content, { events, deprecated, ...kwargs }); |
| 124 | + }); |
| 125 | + |
| 126 | + /** |
| 127 | + * Render the installation instructions for the element |
| 128 | + */ |
| 129 | + eleventyConfig.addPairedAsyncShortcode('renderInstallation', async function(content, kwargs = {}) { |
| 130 | + return `${await render('install.njk', content, kwargs)}`; |
| 131 | + }); |
| 132 | + |
| 133 | + /** |
| 134 | + * Render the list of element methods |
| 135 | + */ |
| 136 | + eleventyConfig.addPairedAsyncShortcode('renderMethods', async function(content, kwargs = {}) { |
| 137 | + const docsPage = this.ctx._; |
| 138 | + const tagName = docsPage.packageTagName(kwargs); |
| 139 | + const { deprecated, current: methods } = docsPage.manifest.getMethods(tagName).reduce(undeprecate, {}); |
| 140 | + return render('methods.njk', content, { methods, deprecated, ...kwargs }); |
| 141 | + }); |
| 142 | + |
| 143 | + /** |
| 144 | + * Render the list of the element's slots |
| 145 | + */ |
| 146 | + eleventyConfig.addPairedAsyncShortcode('renderSlots', async function(content, kwargs = {}) { |
| 147 | + const docsPage = this.ctx._; |
| 148 | + const tagName = docsPage.packageTagName(kwargs); |
| 149 | + const { deprecated, current: slots } = docsPage.manifest.getSlots(tagName).reduce(undeprecate, {}); |
| 150 | + return render('slots.njk', content, { slots, deprecated, ...kwargs }); |
| 151 | + }); |
| 152 | +}; |
0 commit comments