From aa856a5366649a4e0bce5f6aa846a7cfb564f8d4 Mon Sep 17 00:00:00 2001 From: Jackson Harper Date: Tue, 15 Nov 2022 15:34:27 +0800 Subject: [PATCH] If in a pre block dont replace whitespace when highlighting --- .../components/templates/article/ArticleContainer.tsx | 1 - packages/web/lib/highlights/highlightGenerator.ts | 11 +++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/web/components/templates/article/ArticleContainer.tsx b/packages/web/components/templates/article/ArticleContainer.tsx index fb6d3a567..4ee3a3285 100644 --- a/packages/web/components/templates/article/ArticleContainer.tsx +++ b/packages/web/components/templates/article/ArticleContainer.tsx @@ -195,7 +195,6 @@ export function ArticleContainer(props: ArticleContainerProps): JSX.Element { readerTableHeaderColor: theme.colors.readerTableHeader.toString(), readerHeadersColor: theme.colors.readerHeader.toString(), } - console.log('setting font family: ', styles.fontFamily) return ( <> diff --git a/packages/web/lib/highlights/highlightGenerator.ts b/packages/web/lib/highlights/highlightGenerator.ts index 3b2dfab1e..e88979f65 100644 --- a/packages/web/lib/highlights/highlightGenerator.ts +++ b/packages/web/lib/highlights/highlightGenerator.ts @@ -121,10 +121,17 @@ export function makeHighlightNodeAttributes( }) const { parentNode, nextSibling } = node + var isPre = false + var nodeElement = (node instanceof HTMLElement) ? node : node.parentElement + if (nodeElement) { + isPre = (window.getComputedStyle(nodeElement).whiteSpace.startsWith('pre')) + } + parentNode?.removeChild(node) textPartsToHighlight.forEach(({ highlight, text: rawText }, i) => { - // Prevent hardcoded \n, we'll create new-lines based on the startsParagraph data - const text = rawText.replace(/\n/g, '') + // If we are not in preformatted text, prevent hardcoded \n, + // we'll create new-lines based on the startsParagraph data + const text = isPre ? rawText : rawText.replace(/\n/g, '') const newTextNode = document.createTextNode(text) if (!highlight) {