Skip to content

Commit 71b5852

Browse files
authored
fix: count all types of code blocks for explanations (#29749)
* fix: count all types of code blocks for explanations * dx: rm warning on frames plugin This makes the build output unnecessarily chatty. Just keep on chugging along.
1 parent d7e6772 commit 71b5852

2 files changed

Lines changed: 15 additions & 5 deletions

File tree

src/plugins/expressive-code/explain-code.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,14 +157,12 @@ export default () => {
157157
context.renderData.blockAst
158158
);
159159

160+
// Expressive Code frames plugin is required for explain button to work correctly. Ignore if it's not present.
160161
if (
161162
blockAst.tagName !== "figure" ||
162163
!Array.isArray(blockAst.properties?.className) ||
163164
!blockAst.properties.className.includes("frame")
164165
) {
165-
console.warn(
166-
"Expressive Code frames plugin is required for explain button to work correctly",
167-
);
168166
return;
169167
}
170168

src/scripts/explain-code.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
11
import "../components/explain-code-sheet/explain-code-sheet";
22

33
function getCodeBlockPosition(button: HTMLElement): number {
4-
const codeBlocks = document.querySelectorAll(".expressive-code");
5-
const currentBlock = button.closest(".expressive-code");
4+
const wrapperSelector = ".explain";
5+
const codeSelector = "pre code";
6+
const codeBlocks = document.querySelectorAll(codeSelector);
7+
const wrapper = button.closest(wrapperSelector);
8+
9+
let currentBlock = wrapper?.previousElementSibling;
10+
while (currentBlock) {
11+
if (currentBlock.tagName === "PRE") {
12+
currentBlock = currentBlock.querySelector(codeSelector);
13+
console.log(currentBlock);
14+
break;
15+
}
16+
currentBlock = currentBlock.previousElementSibling;
17+
}
618
if (!currentBlock) return 1;
719
return Array.from(codeBlocks).indexOf(currentBlock) + 1;
820
}

0 commit comments

Comments
 (0)