@@ -11,12 +11,16 @@ const RHDSAlphabetizeTagsPlugin = require('./alphabetize-tags.cjs');
11
11
const RHDSShortcodesPlugin = require ( './shortcodes.cjs' ) ;
12
12
const { parse } = require ( 'async-csv' ) ;
13
13
14
- /** @typedef {object } EleventyTransformContext */
14
+ /**
15
+ * @typedef {object } EleventyTransformContext the `this` binding for transform functions
16
+ * @property {string } outputPath the path the page will be written to
17
+ * @property {string } inputPath the path to the page's input file (e.g. template or paginator)
18
+ */
15
19
16
20
/**
17
21
* Replace paths in demo files from the dev SPA's format to 11ty's format
18
22
* @this {EleventyTransformContext}
19
- * @param {string } content
23
+ * @param {string } content the HTML content to replace
20
24
*/
21
25
function demoPaths ( content ) {
22
26
const { outputPath, inputPath } = this ;
@@ -42,18 +46,23 @@ function demoPaths(content) {
42
46
}
43
47
44
48
// Rewrite DEMO lightdom css relative URLs
45
- const LIGHTDOM_HREF_RE = / h r e f = " \. (?< pathname > .* - l i g h t d o m \. c s s ) " / g;
49
+ const LIGHTDOM_HREF_RE = / h r e f = " \. (?< pathname > .* - l i g h t d o m . * \. c s s ) " / g;
46
50
const LIGHTDOM_PATH_RE = / h r e f = " \. ( .* ) " / ;
47
51
48
52
/**
49
- * @param {string | number | Date } dateStr
53
+ * @param {string | number | Date } dateStr iso date string
54
+ * @param {Intl.DateTimeFormatOptions } options date format options
50
55
*/
51
56
function prettyDate ( dateStr , options = { } ) {
52
57
const { dateStyle = 'medium' } = options ;
53
58
return new Intl . DateTimeFormat ( 'en-US' , { dateStyle } )
54
59
. format ( new Date ( dateStr ) ) ;
55
60
}
56
61
62
+ /**
63
+ * @param {string } tagName e.g. pf-jazz-hands
64
+ * @param {import("@patternfly/pfe-tools/config.js").PfeConfig } config pfe tools repo config
65
+ */
57
66
function getTagNameSlug ( tagName , config ) {
58
67
const name = config ?. aliases ?. [ tagName ] ?? tagName . replace ( `${ config ?. tagPrefix ?? 'rh' } -` , '' ) ;
59
68
return slugify ( name , {
@@ -90,7 +99,7 @@ function getFilesToCopy() {
90
99
. filter ( ent => ent . isDirectory ( ) )
91
100
. map ( ent => ent . name ) ;
92
101
93
- /** @type {import('@patternfly/pfe-tools/config.js').PfeConfig }*/
102
+ /** @type {import('@patternfly/pfe-tools/config.js').PfeConfig } */
94
103
const config = require ( '../../.pfe.config.json' ) ;
95
104
96
105
// Copy all component and core files to _site
@@ -103,6 +112,10 @@ function getFilesToCopy() {
103
112
} ) ) ;
104
113
}
105
114
115
+ /**
116
+ * @param {{ slug: number; } } a first
117
+ * @param {{ slug: number; } } b next
118
+ */
106
119
function alphabeticallyBySlug ( a , b ) {
107
120
return (
108
121
a . slug < b . slug ? - 1
@@ -111,7 +124,7 @@ function alphabeticallyBySlug(a, b) {
111
124
) ;
112
125
}
113
126
114
- /** @param {import('@11ty/eleventy/src/UserConfig') } eleventyConfig */
127
+ /** @param {import('@11ty/eleventy/src/UserConfig') } eleventyConfig user config */
115
128
module . exports = function ( eleventyConfig , { tagsToAlphabetize } ) {
116
129
eleventyConfig . addDataExtension ( 'yml, yaml' , contents => yaml . load ( contents ) ) ;
117
130
@@ -145,7 +158,7 @@ module.exports = function(eleventyConfig, { tagsToAlphabetize }) {
145
158
const { pfeconfig } = eleventyConfig ?. globalData ?? { } ;
146
159
const { aliases } = pfeconfig ;
147
160
148
- if ( inputPath === './docs/elements/demos .html' ) {
161
+ if ( inputPath === './docs/elements/demo .html' ) {
149
162
const tagNameMatch = outputPath . match ( / \/ e l e m e n t s \/ (?< tagName > [ - \w ] + ) \/ d e m o \/ / ) ;
150
163
if ( tagNameMatch ) {
151
164
const { tagName } = tagNameMatch . groups ;
@@ -158,17 +171,17 @@ module.exports = function(eleventyConfig, { tagsToAlphabetize }) {
158
171
159
172
// does the tagName exist in the aliases object?
160
173
const key = Object . keys ( modifiedAliases ) . find ( key => modifiedAliases [ key ] === tagName ) ;
161
-
162
- const prefixedTagName = ` ${ pfeconfig ?. tagPrefix } - ${ tagName } ` ;
174
+ const { deslugify } = await import ( '@patternfly/pfe-tools/config.js' ) ;
175
+ const prefixedTagName = deslugify ( tagName , path . join ( __dirname , '../..' ) ) ;
163
176
const redirect = { new : key ?? prefixedTagName , old : tagName } ;
164
177
const matches = content . match ( LIGHTDOM_HREF_RE ) ;
165
178
if ( matches ) {
166
179
for ( const match of matches ) {
167
180
const [ , path ] = match . match ( LIGHTDOM_PATH_RE ) ?? [ ] ;
168
181
const { pathname } = new URL ( path , `file:///${ outputPath } ` ) ;
169
- content = content . replace ( `. ${ path } ` , pathname
170
- . replace ( `/_site/elements/ ${ redirect . old } /` , `/ assets/packages/@rhds/elements/elements/${ redirect . new } /` )
171
- . replace ( '/demo/' , '/' ) ) ;
182
+ const filename = pathname . split ( '/' ) . pop ( ) ;
183
+ const replacement = `/ assets/packages/@rhds/elements/elements/${ prefixedTagName } / ${ filename } ` ;
184
+ content = content . replace ( `. ${ path } ` , replacement ) ;
172
185
}
173
186
}
174
187
}
@@ -189,7 +202,7 @@ module.exports = function(eleventyConfig, { tagsToAlphabetize }) {
189
202
* NB: since the data for this shortcode is no a POJO,
190
203
* but a DocsPage instance, 11ty assigns it to this.ctx._
191
204
* @see https://github.com/11ty/eleventy/blob/bf7c0c0cce1b2cb01561f57fdd33db001df4cb7e/src/Plugins/RenderPlugin.js#L89-L93
192
- * @type {import('@patternfly/pfe-tools/11ty/DocsPage').DocsPage }
205
+ * @type {import('@patternfly/pfe-tools/11ty/DocsPage.js ').DocsPage }
193
206
*/
194
207
const docsPage = this . ctx . _ ;
195
208
return docsPage . description ;
0 commit comments