1
+ import { parse as HTMLParse } from 'node-html-parser' ;
1
2
import * as fs from 'node:fs' ;
2
3
import * as path from 'node:path' ;
3
- import { parse } from 'path ' ;
4
- import { anchorHeadingsPlugin } from './_utils/anchor-headings .js' ;
5
- import { codeExamplesPlugin } from './_utils/code-examples .js' ;
6
- import { copyCodePlugin } from './_utils/copy-code .js' ;
7
- import { currentLink } from './_utils/current-link .js' ;
8
- import { highlightCodePlugin } from './_utils/highlight-code .js' ;
4
+ import { anchorHeadingsTransformer } from './_transformers/anchor-headings.js ' ;
5
+ import { codeExamplesTransformer } from './_transformers/code-examples .js' ;
6
+ import { copyCodeTransformer } from './_transformers/copy-code .js' ;
7
+ import { currentLinkTransformer } from './_transformers/current-link .js' ;
8
+ import { highlightCodeTransformer } from './_transformers/highlight-code .js' ;
9
+ import { outlineTransformer } from './_transformers/outline .js' ;
9
10
import { getComponents } from './_utils/manifest.js' ;
10
11
import { markdown } from './_utils/markdown.js' ;
11
- // import { formatCodePlugin } from './_utils/format-code.js';
12
+ import { SimulateWebAwesomeApp } from './_utils/simulate-webawesome-app.js' ;
13
+ // import { formatCodePlugin } from './_plugins/format-code.js';
12
14
// import litPlugin from '@lit-labs/eleventy-plugin-lit';
13
15
import { readFile } from 'fs/promises' ;
14
- import nunjucks from 'nunjucks' ;
15
16
import process from 'process' ;
16
17
import * as url from 'url' ;
17
- import { outlinePlugin } from './_utils/outline.js' ;
18
- import { replaceTextPlugin } from './_utils/replace-text.js' ;
19
- import { searchPlugin } from './_utils/search.js' ;
18
+ import { replaceTextPlugin } from './_plugins/replace-text.js' ;
19
+ import { searchPlugin } from './_plugins/search.js' ;
20
20
const __dirname = url . fileURLToPath ( new URL ( '.' , import . meta. url ) ) ;
21
21
const isDev = process . argv . includes ( '--develop' ) ;
22
22
const passThroughExtensions = [ 'js' , 'css' , 'png' , 'svg' , 'jpg' , 'mp4' ] ;
23
23
24
+ async function getPackageData ( ) {
25
+ return JSON . parse ( await readFile ( path . join ( __dirname , '..' , 'package.json' ) , 'utf-8' ) ) ;
26
+ }
27
+
24
28
export default async function ( eleventyConfig ) {
25
- const packageData = JSON . parse ( await readFile ( path . join ( __dirname , '..' , 'package.json' ) , 'utf-8' ) ) ;
26
29
const docsDir = path . join ( process . env . BASE_DIR || '.' , 'docs' ) ;
27
- const allComponents = getComponents ( ) ;
30
+ let packageData = await getPackageData ( ) ;
31
+ let allComponents = getComponents ( ) ;
32
+
33
+ const distDir = process . env . UNBUNDLED_DIST_DIRECTORY || path . resolve ( __dirname , '../dist' ) ;
34
+ const customElementsManifest = path . join ( distDir , 'custom-elements.json' ) ;
35
+ const stylesheets = path . join ( distDir , 'styles' ) ;
36
+
37
+ eleventyConfig . addWatchTarget ( customElementsManifest ) ;
38
+ eleventyConfig . setWatchThrottleWaitTime ( 10 ) ; // in milliseconds
39
+
40
+ eleventyConfig . on ( 'eleventy.beforeWatch' , async function ( changedFiles ) {
41
+ let updatePackageData = false ;
42
+ let updateComponentData = false ;
43
+ changedFiles . forEach ( file => {
44
+ if ( file . includes ( 'package.json' ) ) {
45
+ updatePackageData = true ;
46
+ }
47
+
48
+ if ( file . includes ( 'custom-elements.json' ) ) {
49
+ updateComponentData = true ;
50
+ }
51
+ } ) ;
52
+
53
+ if ( updatePackageData ) {
54
+ packageData = await getPackageData ( ) ;
55
+ }
56
+
57
+ if ( updateComponentData ) {
58
+ allComponents = getComponents ( ) ;
59
+ }
60
+ } ) ;
28
61
29
62
/**
30
63
* If you plan to add or remove any of these extensions, make sure to let either Konnor or Cory know as these
@@ -52,7 +85,7 @@ export default async function (eleventyConfig) {
52
85
// Template filters - {{ content | filter }}
53
86
eleventyConfig . addFilter ( 'inlineMarkdown' , content => markdown . renderInline ( content || '' ) ) ;
54
87
eleventyConfig . addFilter ( 'markdown' , content => markdown . render ( content || '' ) ) ;
55
- eleventyConfig . addFilter ( 'stripExtension' , string => parse ( string + '' ) . name ) ;
88
+ eleventyConfig . addFilter ( 'stripExtension' , string => path . parse ( string + '' ) . name ) ;
56
89
eleventyConfig . addFilter ( 'stripPrefix' , content => content . replace ( / ^ w a - / , '' ) ) ;
57
90
// Trims whitespace and pipes from the start and end of a string. Useful for CEM types, which can be pipe-delimited.
58
91
// With Prettier 3, this means a leading pipe will exist be present when the line wraps.
@@ -109,31 +142,6 @@ export default async function (eleventyConfig) {
109
142
return '' ;
110
143
} ) ;
111
144
112
- eleventyConfig . addTransform ( 'second-nunjucks-transform' , function NunjucksTransform ( content ) {
113
- // For a server build, we expect a server to run the second transform.
114
- if ( serverBuild ) {
115
- return content ;
116
- }
117
-
118
- // Only run the transform on files nunjucks would transform.
119
- if ( ! this . page . inputPath . match ( / .( m d | h t m l | n j k ) $ / ) ) {
120
- return content ;
121
- }
122
-
123
- /** This largely mimics what an app would do and just stubs out what we don't care about. */
124
- return nunjucks . renderString ( content , {
125
- // Stub the server EJS shortcodes.
126
- currentUser : {
127
- hasPro : false ,
128
- } ,
129
- server : {
130
- head : '' ,
131
- loginOrAvatar : '' ,
132
- flashes : '' ,
133
- } ,
134
- } ) ;
135
- } ) ;
136
-
137
145
// Paired shortcodes - {% shortCode %}content{% endShortCode %}
138
146
eleventyConfig . addPairedShortcode ( 'markdown' , content => markdown . render ( content || '' ) ) ;
139
147
@@ -152,33 +160,33 @@ export default async function (eleventyConfig) {
152
160
eleventyConfig . setLibrary ( 'md' , markdown ) ;
153
161
154
162
// Add anchors to headings
155
- eleventyConfig . addPlugin ( anchorHeadingsPlugin ( { container : '#content' } ) ) ;
156
-
157
- // Add an outline to the page
158
- eleventyConfig . addPlugin (
159
- outlinePlugin ( {
160
- container : '#content' ,
161
- target : '.outline-links' ,
162
- selector : 'h2, h3' ,
163
- ifEmpty : doc => {
164
- doc . querySelector ( '#outline' ) ?. remove ( ) ;
165
- } ,
166
- } ) ,
167
- ) ;
168
-
169
- // Add current link classes
170
- eleventyConfig . addPlugin ( currentLink ( ) ) ;
171
-
172
- // Add code examples for `<code class="example">` blocks
173
- eleventyConfig . addPlugin ( codeExamplesPlugin ( ) ) ;
174
-
175
- // Highlight code blocks with Prism
176
- eleventyConfig . addPlugin ( highlightCodePlugin ( ) ) ;
163
+ eleventyConfig . addTransform ( 'doc-transforms' , function ( content ) {
164
+ let doc = HTMLParse ( content , { blockTextElements : { code : true } } ) ;
165
+
166
+ const transformers = [
167
+ anchorHeadingsTransformer ( { container : '#content' } ) ,
168
+ outlineTransformer ( {
169
+ container : '#content' ,
170
+ target : '.outline-links' ,
171
+ selector : 'h2, h3' ,
172
+ ifEmpty : doc => {
173
+ doc . querySelector ( '#outline' ) ?. remove ( ) ;
174
+ } ,
175
+ } ) ,
176
+ // Add current link classes
177
+ currentLinkTransformer ( ) ,
178
+ codeExamplesTransformer ( ) ,
179
+ highlightCodeTransformer ( ) ,
180
+ copyCodeTransformer ( ) ,
181
+ ] ;
182
+
183
+ for ( const transformer of transformers ) {
184
+ transformer . call ( this , doc ) ;
185
+ }
177
186
178
- // Add copy code buttons to code blocks
179
- eleventyConfig . addPlugin ( copyCodePlugin ) ;
187
+ return doc . toString ( ) ;
188
+ } ) ;
180
189
181
- // Various text replacements
182
190
eleventyConfig . addPlugin (
183
191
replaceTextPlugin ( [
184
192
{
@@ -222,15 +230,14 @@ export default async function (eleventyConfig) {
222
230
// }
223
231
224
232
let assetsDir = path . join ( process . env . BASE_DIR || 'docs' , 'assets' ) ;
225
- fs . cpSync ( assetsDir , path . join ( eleventyConfig . directories . output , 'assets' ) , { recursive : true } ) ;
233
+ const siteAssetsDir = path . join ( eleventyConfig . directories . output , 'assets' ) ;
234
+ fs . cpSync ( assetsDir , siteAssetsDir , { recursive : true } ) ;
226
235
227
236
for ( let glob of passThrough ) {
228
237
eleventyConfig . addPassthroughCopy ( glob ) ;
229
238
}
230
239
231
240
// // SSR plugin
232
- // // Make sure this is the last thing, we don't want to run the risk of accidentally transforming shadow roots with
233
- // // the nunjucks 2nd transform.
234
241
// if (!isDev) {
235
242
// //
236
243
// // Problematic components in SSR land:
@@ -253,6 +260,23 @@ export default async function (eleventyConfig) {
253
260
// componentModules,
254
261
// });
255
262
// }
263
+
264
+ if ( ! isDev ) {
265
+ eleventyConfig . addTransform ( 'simulate-webawesome-app' , function ( content ) {
266
+ // For a server build, we expect a server to run the second transform.
267
+ if ( serverBuild ) {
268
+ return content ;
269
+ }
270
+
271
+ // Only run the transform on files nunjucks would transform.
272
+ if ( ! this . page . inputPath . match ( / .( m d | h t m l | n j k ) $ / ) ) {
273
+ return content ;
274
+ }
275
+
276
+ /** This largely mimics what an app would do and just stubs out what we don't care about. */
277
+ return SimulateWebAwesomeApp ( content ) ;
278
+ } ) ;
279
+ }
256
280
}
257
281
258
282
export const config = {
0 commit comments