Skip to content

Commit 699a812

Browse files
fix(tools): prevent duplicate ids on docs pages (#2527)
* fix(tools): add weakmap counter DocsPage * refactor: simplify styling hooks guard * docs: changeset --------- Co-authored-by: Benny Powers <[email protected]>
1 parent b7a9556 commit 699a812

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
"@patternfly/pfe-tools": patch
3+
---
4+
`11ty`: prevent duplicate IDs on code pages when multiple elements are documented

tools/pfe-tools/11ty/DocsPage.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ export interface RenderKwargs {
2424
level?: number;
2525
}
2626

27+
/**
28+
* docs pages contain a #styling-hooks anchor as back compat for older versions of the page
29+
* to prevent this id from rendering more than once, we track the number of times each page
30+
* renders css custom properties.
31+
*/
32+
const cssStylingHookIdTracker = new WeakSet();
33+
2734
export declare class DocsPageRenderer {
2835
public tagName: string;
2936
public manifest: Manifest;
@@ -136,13 +143,15 @@ export class DocsPage implements DocsPageRenderer {
136143
return this.templates.render('properties.njk', { content, properties, deprecated, ...kwargs });
137144
}
138145

139-
/** Render a talbe of element CSS Custom Properties */
146+
/** Render a table of element CSS Custom Properties */
140147
renderCssCustomProperties(content: string, kwargs: RenderKwargs = {}) {
148+
const hasStylingHooks = cssStylingHookIdTracker.has(this);
149+
cssStylingHookIdTracker.add(this);
141150
const allCssProperties = this.manifest.getCssCustomProperties(this.#packageTagName(kwargs)) ?? [];
142151
const cssProperties = allCssProperties.filter(x => !x.deprecated);
143152
const deprecated = allCssProperties.filter(x => x.deprecated);
144153

145-
return this.templates.render('css-custom-properties.njk', { content, cssProperties, deprecated, ...kwargs });
154+
return this.templates.render('css-custom-properties.njk', { content, cssProperties, deprecated, hasStylingHooks, ...kwargs });
146155
}
147156

148157
/** Render the list of element CSS Shadow Parts */
@@ -152,7 +161,7 @@ export class DocsPage implements DocsPageRenderer {
152161
const parts = allParts.filter(x => !x.deprecated);
153162
const deprecated = allParts.filter(x => x.deprecated);
154163

155-
return this.templates.render('css-shadow-parts.njk', { parts, deprecated, content, ...kwargs, });
164+
return this.templates.render('css-shadow-parts.njk', { parts, deprecated, content, ...kwargs });
156165
}
157166

158167
/** Render the list of events for the element */

tools/pfe-tools/11ty/templates/css-custom-properties.njk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
{%- endmacro -%}
1111

1212
<section class="api band css-custom-properties api-properties">
13-
<a id="styling-hooks"></a>
13+
{% if not hasStylingHooks %}<a id="styling-hooks"></a>{% endif %}
1414
{{ header | mdHeading(level) | safe }} {% if not content and not cssProperties.length %}
1515

1616
<em>None</em> {%- else -%}

0 commit comments

Comments
 (0)