Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Code Blocks] Allow <pre> to contain <code> child #147

Merged
merged 1 commit into from
Jan 11, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src_js/components/main_content/useEnhancedCodeBlocks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export default function useEnhancedCodeBlocks(
// Then attempt to enhance ordinary <pre> blocks.
enhanceBlocks(
mainElRef.current.querySelectorAll('pre'),
(codeblock: HTMLElement) => codeblock.innerHTML.trim(),
getRawContentsFromPreCodeblock,
numCodeBlocks,
);

Expand Down Expand Up @@ -90,6 +90,25 @@ function getRawContentsFromJekyllRougeCodeblock(
return (codeEl as HTMLElement).innerHTML;
}

function getRawContentsFromPreCodeblock(
codeblock_: HTMLElement,
): string | null {
let codeblock = codeblock_;
// The structure of a <pre> codeblock:
// <pre>
// <code> <!-- OPTIONAL -->
// [contents]
// </code>
// </pre>
if (
codeblock.childNodes.length === 1 &&
codeblock.firstElementChild?.tagName === 'CODE'
) {
codeblock = codeblock.firstElementChild as HTMLElement;
}
return codeblock.innerHTML.trim();
}

/**
* @param codeblocks Output from `.querySelectorAll()`
* @param getContents A method that extracts a string with the codeblock contents given a codeblock element
Expand Down