Skip to content

Commit

Permalink
feat(markdown): support syntax highlighting (#265)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kohei Asai authored Mar 21, 2021
1 parent 83dd9cb commit 6b8ee88
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 51 deletions.
138 changes: 88 additions & 50 deletions components/code-block.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { css, cx } from "@linaria/core";
import Highlight, { defaultProps } from "prism-react-renderer";
import * as React from "react";
import { useIntl } from "react-intl";

Expand Down Expand Up @@ -59,58 +60,95 @@ export const CodeBlock: React.VFC<CodeBlockProps> = ({
}
`}
>
<code
className={css`
color: #cfcfcf;
& .selector,
& .keyword,
& .operator {
color: #ff6b6b;
<Highlight {...defaultProps} code={value} language={language as any}>
{({ className, tokens, getTokenProps }) => {
const elements: React.ReactElement[] = [];

for (const [i, line] of tokens.entries()) {
console.log(i, line);

for (const [j, token] of line.entries()) {
const { className, children } = getTokenProps({ token, j });

// prism outputs single new-line character node for empty line
// remove this since we manually add new-line character element for each line
if (children === "\n") {
continue;
}

elements.push(
<span className={className} key={`${i}-${j}`}>
{children}
</span>
);
}

elements.push(
<span className="token plain" key={`${i}-nl`}>
{"\n"}
</span>
);
}

& .property {
color: #54a0ff;
}
& .parameter {
color: #abb2bf;
}
& .attr-name,
& .function {
color: #ff9ff3;
}
& .punctuation {
color: #abb2bf;
}
& .class-name,
& .maybe-class-name {
color: #feca57;
}
& .constant,
& .number,
& .unit,
& .interpolation,
& .pseudo-element {
color: #ff9f43;
}
& .string,
& .attr-value {
color: #1dd1a1;
}
& .hexcode {
color: #00d2d3;
}
& .comment {
color: #576574;
}
`}
ref={codeRef}
>
{value}
</code>
return (
<code
className={cx(
css`
color: #cfcfcf;
& .selector,
& .keyword,
& .operator {
color: #ff6b6b;
}
& .property {
color: #54a0ff;
}
& .parameter {
color: #abb2bf;
}
& .attr-name,
& .function {
color: #ff9ff3;
}
& .punctuation {
color: #abb2bf;
}
& .class-name,
& .maybe-class-name {
color: #feca57;
}
& .constant,
& .number,
& .unit,
& .interpolation,
& .pseudo-element {
color: #ff9f43;
}
& .string,
& .attr-value {
color: #1dd1a1;
}
& .hexcode {
color: #00d2d3;
}
& .comment {
color: #576574;
}
`,
className
)}
ref={codeRef}
>
{elements}
</code>
);
}}
</Highlight>
</div>

<button
Expand Down
6 changes: 5 additions & 1 deletion components/localized-link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ export const LocalizedLink: React.FC<LocalizedLinkProps> = ({

return (
<Link
href={{ pathname: hrefUrl.pathname, search: hrefUrl.search }}
href={{
pathname: hrefUrl.pathname,
search: hrefUrl.search,
hash: hrefUrl.hash,
}}
as={{ pathname: asUrl.pathname, search: asUrl.search, hash: asUrl.hash }}
{...props}
/>
Expand Down
15 changes: 15 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"next": "^10.0.8",
"normalize.css": "^8.0.1",
"polished": "^4.1.1",
"prism-react-renderer": "^1.2.0",
"react": "^16.14.0",
"react-dom": "^16.14.0",
"react-intl": "^5.13.2",
Expand Down

1 comment on commit 6b8ee88

@vercel
Copy link

@vercel vercel bot commented on 6b8ee88 Mar 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.