Skip to content

Commit

Permalink
Add code syntax highlighting (#1375)
Browse files Browse the repository at this point in the history
  • Loading branch information
aeharding authored Mar 28, 2024
1 parent 2162492 commit b2e2931
Show file tree
Hide file tree
Showing 6 changed files with 219 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@
"react-router-dom": "^5.3.4",
"react-textarea-autosize": "^8.5.3",
"react-transition-state": "^2.1.1",
"rehype-highlight": "^7.0.0",
"rehype-parse": "^9.0.0",
"rehype-remark": "^10.0.0",
"release-it": "^17.1.1",
Expand Down
26 changes: 26 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/core/globalCssOverrides.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { css } from "@linaria/core";

import "./syntaxHighlightCss";

export default css`
:global() {
:root {
Expand Down
170 changes: 170 additions & 0 deletions src/core/syntaxHighlightCss.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
import { css } from "@linaria/core";

export default css`
:global() {
html.ion-palette-dark {
.hljs-doctag,
.hljs-keyword,
.hljs-meta .hljs-keyword,
.hljs-template-tag,
.hljs-template-variable,
.hljs-type,
.hljs-variable.language_ {
color: #ff7b72;
}
.hljs-title,
.hljs-title.class_,
.hljs-title.class_.inherited__,
.hljs-title.function_ {
color: #d2a8ff;
}
.hljs-attr,
.hljs-attribute,
.hljs-literal,
.hljs-meta,
.hljs-number,
.hljs-operator,
.hljs-variable,
.hljs-selector-attr,
.hljs-selector-class,
.hljs-selector-id {
color: #79c0ff;
}
.hljs-regexp,
.hljs-string,
.hljs-meta .hljs-string {
color: #a5d6ff;
}
.hljs-built_in,
.hljs-symbol {
color: #ffa657;
}
.hljs-comment,
.hljs-code,
.hljs-formula {
color: #8b949e;
}
.hljs-name,
.hljs-quote,
.hljs-selector-tag,
.hljs-selector-pseudo {
color: #7ee787;
}
.hljs-subst {
color: #c9d1d9;
}
.hljs-section {
color: #1f6feb;
font-weight: bold;
}
.hljs-bullet {
color: #f2cc60;
}
.hljs-emphasis {
color: #c9d1d9;
font-style: italic;
}
.hljs-strong {
color: #c9d1d9;
font-weight: bold;
}
.hljs-addition {
color: #aff5b4;
background-color: #033a16;
}
.hljs-deletion {
color: #ffdcd7;
background-color: #67060c;
}
}
html:not(.ion-palette-dark) {
.hljs-doctag,
.hljs-keyword,
.hljs-meta .hljs-keyword,
.hljs-template-tag,
.hljs-template-variable,
.hljs-type,
.hljs-variable.language_ {
color: #d73a49;
}
.hljs-title,
.hljs-title.class_,
.hljs-title.class_.inherited__,
.hljs-title.function_ {
color: #6f42c1;
}
.hljs-attr,
.hljs-attribute,
.hljs-literal,
.hljs-meta,
.hljs-number,
.hljs-operator,
.hljs-variable,
.hljs-selector-attr,
.hljs-selector-class,
.hljs-selector-id {
color: #005cc5;
}
.hljs-regexp,
.hljs-string,
.hljs-meta .hljs-string {
color: #032f62;
}
.hljs-built_in,
.hljs-symbol {
color: #e36209;
}
.hljs-comment,
.hljs-code,
.hljs-formula {
color: #6a737d;
}
.hljs-name,
.hljs-quote,
.hljs-selector-tag,
.hljs-selector-pseudo {
color: #22863a;
}
.hljs-subst {
color: #24292e;
}
.hljs-section {
color: #005cc5;
font-weight: bold;
}
.hljs-bullet {
color: #735c0f;
}
.hljs-emphasis {
color: #24292e;
font-style: italic;
}
.hljs-strong {
color: #24292e;
font-weight: bold;
}
.hljs-addition {
color: #22863a;
background-color: #f0fff4;
}
.hljs-deletion {
color: #b31d28;
background-color: #ffeef0;
}
}
}
`;
4 changes: 4 additions & 0 deletions src/features/post/inFeed/large/LargePostContents.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ const PostBody = styled.div`
-webkit-line-clamp: 3;
-webkit-box-orient: vertical;
overflow: hidden;
&:empty {
display: none;
}
`;

const postBodyReadCss = css`
Expand Down
16 changes: 16 additions & 0 deletions src/features/shared/markdown/Markdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import spoiler from "@aeharding/remark-lemmy-spoiler";
import Summary from "./components/spoiler/Summary";
import Details from "./components/spoiler/Details";
import spoilerRehype from "./spoilerRehype";
import rehypeHighlight from "rehype-highlight";

const markdownCss = css`
@media (max-width: 700px) {
Expand All @@ -24,6 +25,20 @@ const markdownCss = css`
white-space: pre-wrap;
}
code:not(.hljs) {
background: var(--lightroom-bg);
border-radius: 4px;
padding: 1px 3px;
color: rgba(var(--ion-color-dark-rgb), 0.9);
}
pre {
background: var(--lightroom-bg);
padding: 8px;
border-radius: 8px;
font-size: 0.9375em;
}
blockquote {
padding-left: 0.5rem;
border-left: 3px solid
Expand Down Expand Up @@ -103,6 +118,7 @@ export default function Markdown({
spoiler,
spoilerRehype,
]}
rehypePlugins={[[rehypeHighlight, { detect: true }]]}
/>
);
}

0 comments on commit b2e2931

Please sign in to comment.