Skip to content

Commit

Permalink
Add/fix some styles of the code blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmz committed Jul 14, 2024
1 parent 9e4438b commit 6d162d1
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 21 deletions.
11 changes: 7 additions & 4 deletions src/components/code-block.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@ import styles from './code-block.module.scss';
export default function CodeBlock({ text, className = '' }) {
const clsName = cn(styles.codeBlock, className);

const [, start, body, end] = /^([^\n]+)(.*?)([^\n]+)$/s.exec(text);

return (
<>
<span className="p-break">
<br /> <br />
</span>
<pre className={clsName}>
<code>{text}</code>
<code>
<span className="code--backticks">{start}</span>
{body}
<span className="code--backticks">{end}</span>
</code>
</pre>
<span className="p-break">
<br /> <br />
</span>
</>
);
}
Expand Down
3 changes: 2 additions & 1 deletion src/components/code-block.module.scss
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
.codeBlock {
display: inline;
padding: unset;
font-size: 90%;
word-break: auto-phrase;
white-space: pre-wrap;
background-color: transparent;
border: none;
position: relative;
margin: unset;
color: inherit;
font-size: 100%; // Compensate the Bootstrap font size

:global(.dark-theme) & code {
background-color: unset;
Expand Down
8 changes: 6 additions & 2 deletions src/components/linkify-elements.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,16 @@ export function tokenToElement(token, key, params) {
case INITIAL_CHECKBOX:
return <InitialCheckbox key={key} checked={isChecked(token.text)} />;

case CODE_INLINE:
case CODE_INLINE: {
const [, start, text, end] = /^(`+)(.*)(\1)$/.exec(token.text);
return (
<code key={key} className="inline-code">
{token.text}
<span className="code--backticks">{start}</span>
{text}
<span className="code--backticks">{end}</span>
</code>
);
}

case CODE_BLOCK:
return <CodeBlock key={key} text={token.text} />;
Expand Down
9 changes: 9 additions & 0 deletions styles/shared/post.scss
Original file line number Diff line number Diff line change
Expand Up @@ -266,12 +266,21 @@ code.inline-code {
color: unset;
background-color: unset;
padding: unset;
font-size: 100%; // Compensate the Bootstrap font size

.dark-theme & {
background-color: unset;
}
}

.code--backticks {
color: #999;

.dark-theme & {
color: $text-color-darker;
}
}

.text-no-breaks br {
display: none;
}
Expand Down
33 changes: 23 additions & 10 deletions test/jest/__snapshots__/piece-of-text.test.jsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,12 @@ exports[`PieceOfText > Renders text with code block 1`] = `
class="codeBlock"
>
<code>
\`\`\`
<span
class="code--backticks"
>
\`\`\`
</span>
1+1=2;
foo();
@mention
Expand All @@ -122,16 +127,14 @@ [email protected]
^
&lt;spoiler&gt;https://example.com
\`\`\`
<span
class="code--backticks"
>
\`\`\`
</span>
</code>
</pre>
<span
class="p-break"
>
<br />
<br />
</span>
</span>
</DocumentFragment>
`;
Expand All @@ -149,7 +152,17 @@ exports[`PieceOfText > Renders text with inline code 1`] = `
<code
class="inline-code"
>
\`1+1=2; foo(); @mention [email protected] #hashtag ^ &lt;spoiler&gt;https://example.com\`
<span
class="code--backticks"
>
\`
</span>
1+1=2; foo(); @mention [email protected] #hashtag ^ &lt;spoiler&gt;https://example.com
<span
class="code--backticks"
>
\`
</span>
</code>
</span>
Expand Down
11 changes: 7 additions & 4 deletions test/unit/components/piece-of-text.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,20 @@ describe('<PieceOfText>', () => {
});

it('should correctly process texts with inline code', () => {
const code =
'`1+1=2; foo(); @mention [email protected] #hashtag ^ <spoiler>https://example.com`';
const text = `Here is the code ${code}. </spoiler> Read it carefully`;
const code = '1+1=2; foo(); @mention [email protected] #hashtag ^ <spoiler>https://example.com';
const text = `Here is the code \`${code}\`. </spoiler> Read it carefully`;
expect(
<Linkify>{text}</Linkify>,
'when rendered',
'to have rendered with all children',
<span>
<ErrorBoundary>
{'Here is the code '}
<code>{code}</code>
<code>
<span className="code--backticks">`</span>
{code}
<span className="code--backticks">`</span>
</code>
{'. </spoiler> Read it carefully'}
</ErrorBoundary>
</span>,
Expand Down

0 comments on commit 6d162d1

Please sign in to comment.