Skip to content

Commit bd977f5

Browse files
committed
Refactor attribute link generation and improve formatting in validateAttributeAnchors function
1 parent 92bde9c commit bd977f5

1 file changed

Lines changed: 33 additions & 26 deletions

File tree

tools/api-docs/generate-block-docs.mjs

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,8 @@ function validateAttributeAnchors() {
157157

158158
const content = fs.readFileSync( ATTRIBUTES_DOC_PATH, 'utf-8' );
159159
const anchors = new Set(
160-
Array.from(
161-
content.matchAll( /^#{1,6}\s+(.+)$/gm ),
162-
( m ) => slugifyHeading( m[ 1 ] )
160+
Array.from( content.matchAll( /^#{1,6}\s+(.+)$/gm ), ( m ) =>
161+
slugifyHeading( m[ 1 ] )
163162
)
164163
);
165164

@@ -179,6 +178,17 @@ function validateAttributeAnchors() {
179178

180179
// ─── Formatting helpers ─────────────────────────────────────────────────────
181180

181+
/**
182+
* Build a markdown link to a specific anchor on the Block Attributes docs page.
183+
*
184+
* @param {string} label Display text for the link.
185+
* @param {string} anchor Key in ATTRIBUTE_ANCHORS.
186+
* @return {string} Markdown link.
187+
*/
188+
function attrLink( label, anchor ) {
189+
return `[${ label }](${ ATTRIBUTES_REF }#${ ATTRIBUTE_ANCHORS[ anchor ] })`;
190+
}
191+
182192
/**
183193
* Format attributes as a Markdown table.
184194
*
@@ -190,11 +200,11 @@ function formatAttributesTable( attributes ) {
190200
return '_This block has no custom attributes._';
191201
}
192202

193-
const attrLink = ( label, anchor ) =>
194-
`[${ label }](${ ATTRIBUTES_REF }#${ ATTRIBUTE_ANCHORS[ anchor ] })`;
195-
196203
const rows = [
197-
`| Attribute | ${ attrLink( 'Type', 'type' ) } | ${ attrLink( 'Default', 'default' ) } | Description |`,
204+
`| Attribute | ${ attrLink( 'Type', 'type' ) } | ${ attrLink(
205+
'Default',
206+
'default'
207+
) } | Description |`,
198208
'|-----------|------|---------|-------------|',
199209
];
200210

@@ -208,33 +218,30 @@ function formatAttributesTable( attributes ) {
208218
: '—';
209219

210220
const descParts = [];
211-
if ( attrDef.source ) {
212-
descParts.push(
213-
`${ attrLink( 'Source', 'source' ) }: \`${ attrDef.source }\``
214-
);
215-
}
216-
if ( attrDef.selector ) {
217-
descParts.push(
218-
`${ attrLink( 'Selector', 'selector' ) }: \`${ attrDef.selector }\``
219-
);
220-
}
221-
if ( attrDef.attribute ) {
222-
descParts.push(
223-
`${ attrLink( 'HTML attr', 'attribute' ) }: \`${ attrDef.attribute }\``
224-
);
221+
222+
// Simple key-value description fields.
223+
const simpleFields = [
224+
[ 'source', 'Source' ],
225+
[ 'selector', 'Selector' ],
226+
[ 'attribute', 'HTML attr' ],
227+
[ 'role', 'Role' ],
228+
];
229+
for ( const [ field, label ] of simpleFields ) {
230+
if ( attrDef[ field ] ) {
231+
descParts.push(
232+
`${ attrLink( label, field ) }: \`${ attrDef[ field ] }\``
233+
);
234+
}
225235
}
236+
237+
// Enum needs special formatting (list of values).
226238
if ( attrDef.enum ) {
227239
descParts.push(
228240
`${ attrLink( 'Enum', 'enum' ) }: ${ attrDef.enum
229241
.map( ( v ) => `\`${ v }\`` )
230242
.join( ', ' ) }`
231243
);
232244
}
233-
if ( attrDef.role ) {
234-
descParts.push(
235-
`${ attrLink( 'Role', 'role' ) }: \`${ attrDef.role }\``
236-
);
237-
}
238245

239246
const desc = descParts.length > 0 ? descParts.join( '. ' ) : '—';
240247
rows.push(

0 commit comments

Comments
 (0)