Skip to content

Commit 387be22

Browse files
josh-ramos-22Josh Ramos
andauthored
NEXT - Format Code Blocks on Blog Posts (#373)
* initial changes to allow formatting of content in blog * basic solution without syntax highlighting * potential fix to pipeline error (?) * basic code block with syntax highlighting complete * temp fix for scrolling lines in codeblocks * fixed wrapping issue * removed php support - issue with PrismJs library * fix to word wrap issue * added margin to footer to add some space when a blogpost ends with a code block * removed rogue print statement * removed rogue print statement (again) * removed deprecated code --------- Co-authored-by: Josh Ramos <josh.ramos@student.unsw.edu.au>
1 parent a183990 commit 387be22

File tree

9 files changed

+189
-17
lines changed

9 files changed

+189
-17
lines changed

frontend/src/packages/editor/components/CodeBlock.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import 'prismjs/components/prism-typescript';
2626
import 'prismjs/components/prism-tsx';
2727
import 'prismjs/components/prism-markdown';
2828
import 'prismjs/components/prism-python';
29-
import 'prismjs/components/prism-php';
3029
import 'prismjs/components/prism-sql';
3130
import 'prismjs/components/prism-java';
3231
import 'prismjs/components/prism-c';
@@ -87,7 +86,6 @@ const LanguageSelect = (props: JSX.IntrinsicElements['select']) => {
8786
<option value="markdown">Markdown</option>
8887
<option value="c">C</option>
8988
<option value="cpp">C++</option>
90-
<option value="php">PHP</option>
9189
<option value="python">Python</option>
9290
<option value="sql">SQL</option>
9391
<option value="bash">Shell</option>

next/package-lock.json

Lines changed: 58 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

next/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@
1313
"@emotion/react": "11.10.4",
1414
"@emotion/styled": "11.10.4",
1515
"@mui/material": "5.10.6",
16+
"@types/prismjs": "^1.26.0",
1617
"@types/styled-components": "5.1.25",
1718
"framer-motion": "7.3.6",
1819
"next": "12.3.1",
20+
"prismjs": "^1.29.0",
1921
"react": "18.2.0",
2022
"react-dom": "18.2.0",
2123
"react-is": "18.2.0",
24+
"redux-persist": "^6.0.0",
2225
"sharp": "0.31.0",
2326
"styled-components": "5.3.5"
2427
},

next/src/components/blog/Blog-styled.tsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,27 @@ const ParagraphContainer = styled.div`
5050
}
5151
`;
5252

53+
const CodeContainer = styled.div`
54+
margin: 0em;
55+
padding-left: 0.5em;
56+
font-family: monospace;
57+
background: #f5f2f0;
58+
`
59+
60+
const CodeLineWrapper = styled.pre`
61+
margin: 0px !important;
62+
padding: 1.5px !important;
63+
overflow: hide !important;
64+
`
65+
66+
const CodeLine = styled.code`
67+
margin: 0px !important;
68+
padding: 0px !important;
69+
font-size: 0.85rem !important;
70+
white-space: pre-wrap !important;
71+
word-break: break-word !important;
72+
`
73+
5374
const BlogContainer = styled.div`
5475
font-size: 1.25rem;
5576
margin: 0px 60px;
@@ -77,4 +98,7 @@ export {
7798
BlogContainer,
7899
BlogHeading,
79100
ParagraphContainer,
101+
CodeContainer,
102+
CodeLine,
103+
CodeLineWrapper
80104
};

next/src/components/blog/Blog.tsx

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,63 @@ import {
66
ImagePlaceholder,
77
AlignedText,
88
BlogContainer,
9+
CodeContainer,
10+
CodeLine,
11+
CodeLineWrapper,
912
} from "./Blog-styled";
1013
import type { Element, Block } from "./types";
1114

15+
import Prism from 'prismjs';
16+
import 'prismjs/themes/prism.css';
17+
import { useEffect } from "react";
18+
19+
// Supported Languages
20+
//
21+
// For all languages supported by Prism, visit https://prismjs.com/
22+
import 'prismjs/components/prism-javascript';
23+
import 'prismjs/components/prism-jsx';
24+
import 'prismjs/components/prism-typescript';
25+
import 'prismjs/components/prism-tsx';
26+
import 'prismjs/components/prism-markdown';
27+
import 'prismjs/components/prism-python';
28+
import 'prismjs/components/prism-sql';
29+
import 'prismjs/components/prism-java';
30+
import 'prismjs/components/prism-c';
31+
import 'prismjs/components/prism-cpp';
32+
import 'prismjs/components/prism-csharp';
33+
import 'prismjs/components/prism-prolog';
34+
import 'prismjs/components/prism-bash';
35+
import 'prismjs/components/prism-latex';
36+
import 'prismjs/components/prism-rust';
37+
import 'prismjs/components/prism-go'
38+
import 'prismjs/components/prism-haskell';
39+
import 'prismjs/components/prism-perl';
40+
1241
const Block = ({ element }: { element: Element }) => {
42+
useEffect(() => {
43+
Prism.highlightAll();
44+
}, []);
45+
1346
if (element.type === "image") {
1447
return <ImagePlaceholder>{element.url}</ImagePlaceholder>;
1548
}
49+
50+
if (element.type === "code") {
51+
const language = "language-" + (element.language ?? "python");
52+
53+
return (
54+
<CodeContainer>
55+
{element.children.map(({ text, ...textStyle }, idx) => (
56+
<CodeLineWrapper key={idx}>
57+
<CodeLine className={language}>
58+
{text}
59+
</CodeLine>
60+
</CodeLineWrapper>
61+
))}
62+
</CodeContainer>
63+
)
64+
}
65+
1666
return (
1767
<ParagraphContainer>
1868
{element.children.map(({ text, link, ...textStyle }, idx) => (

next/src/components/blog/types.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export interface Document {
66

77
export type Block = Element[];
88

9-
export type Element = Paragraph | Image;
9+
export type Element = Paragraph | Image | Code;
1010

1111
interface Paragraph {
1212
type: "paragraph";
@@ -18,11 +18,22 @@ interface Image {
1818
url: string;
1919
}
2020

21+
interface Code {
22+
type: "code";
23+
language: String;
24+
children: CodeLine[]
25+
}
26+
2127
interface Text extends TextStyle {
2228
text: string;
2329
link?: string;
2430
}
2531

32+
interface CodeLine extends TextStyle {
33+
text: string;
34+
language: string;
35+
}
36+
2637
export interface TextStyle {
2738
bold?: boolean;
2839
italic?: boolean;

next/src/components/footer/Footer.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const FooterComponent = styled.footer`
2929
background-color: #A09FE3;
3030
padding: 2rem;
3131
display: flex;
32+
margin-top: 1.5em;
3233
flex-direction: column;
3334
3435
@media ${device.tablet} {

next/src/pages/blog/[bid].tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export const getServerSideProps: GetServerSideProps = async ({ params }) => {
4949
method: "GET",
5050
}
5151
).then((res) => res.text());
52+
5253
return { props: { data: JSON.parse(data).Contents } };
5354
};
5455

0 commit comments

Comments
 (0)