Skip to content

Commit 4434b9d

Browse files
committed
Anchors visible but link checker still failing
1 parent c785aec commit 4434b9d

File tree

1 file changed

+37
-8
lines changed

1 file changed

+37
-8
lines changed

plugins/remark-custom-blocks.js

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,30 +52,30 @@ const plugin = (options) => {
5252
let currentStepContent = [];
5353
let currentStepLabel = null;
5454
let currentStepId = null;
55-
// No anchorId variable here
55+
let currentAnchorId = null;
5656

5757
const finalizeStep = () => {
5858
if (currentStepLabel) {
5959
stepsData.push({
6060
id: currentStepId, // step-X ID
6161
label: currentStepLabel, // Plain text label
62-
// No anchorId here
62+
anchorId: currentAnchorId,
6363
content: [...currentStepContent],
6464
});
65-
console.log(`Finalized step: ${currentStepLabel}`); // Log from original code
6665
}
6766
currentStepContent = [];
6867
currentStepLabel = null; // Reset label
6968
};
7069

7170
if (node.children && node.children.length > 0) {
7271
node.children.forEach((child) => {
72+
console.log(child)
7373
if (child.type === 'heading' && child.depth === 2) {
7474
finalizeStep(); // Finalize the previous step first
7575
currentStepLabel = extractText(child.children);
76+
currentAnchorId = child.data.hProperties.id;
7677
currentStepId = `step-${stepsData.length + 1}`; // Generate step-X ID
77-
// No anchor extraction here
78-
console.log(`Found heading: ${currentStepLabel}`); // Log from original code
78+
console.log(`Found heading: ${currentStepLabel} \{#${currentAnchorId}\}`); // Log from original code
7979
} else if (currentStepLabel) {
8080
// Only collect content nodes *after* a heading has defined a step
8181
currentStepContent.push(child);
@@ -110,7 +110,6 @@ const plugin = (options) => {
110110
const stepAttributes = [
111111
{ type: 'mdxJsxAttribute', name: 'id', value: step.id }, // step-X
112112
{ type: 'mdxJsxAttribute', name: 'label', value: step.label }, // Plain text
113-
// No anchorId attribute
114113
];
115114

116115
// Add forceExpanded attribute if parent was expanded
@@ -123,14 +122,44 @@ const plugin = (options) => {
123122
});
124123
}
125124

125+
const anchorElement = {
126+
type: 'mdxJsxFlowElement',
127+
name: 'a',
128+
attributes: [
129+
{ type: 'mdxJsxAttribute', name: 'href', value: `#${step.anchorId}` },
130+
{ type: 'mdxJsxAttribute', name: 'className', value: 'hash-link' }
131+
],
132+
children: [
133+
// Add a link symbol or text
134+
{
135+
type: 'text',
136+
value: '🔗' // Or any other symbol/text you prefer
137+
}
138+
]
139+
};
140+
141+
const contentWithAnchor = [
142+
{
143+
type: 'mdxJsxFlowElement',
144+
name: 'div',
145+
attributes: [
146+
{ type: 'mdxJsxAttribute', name: 'id', value: step.anchorId }
147+
],
148+
children: [
149+
anchorElement, // Add the anchor element
150+
...step.content // Then add the regular content
151+
]
152+
}
153+
];
154+
126155
// Push the Step node
127156
node.children.push({
128157
type: 'mdxJsxFlowElement',
129158
name: 'Step', // Output Step tag
130159
attributes: stepAttributes,
131-
children: step.content, // Pass content nodes as children
160+
children: contentWithAnchor, // Pass content nodes as children
132161
});
133-
console.log(`Added step: ${step.label}`); // Log from original code
162+
console.log(`Added step: ${step.label} with anchorId: ${step.anchorId || 'none'}`); // Log from original code
134163
});
135164
} catch (error) {
136165
const filePath = file?.path || 'unknown file';

0 commit comments

Comments
 (0)