Skip to content

Commit

Permalink
Adds hightligh to glossary code examples
Browse files Browse the repository at this point in the history
  • Loading branch information
RodrigoEC committed Sep 2, 2024
1 parent e32c808 commit b0173f8
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 88 deletions.
24 changes: 24 additions & 0 deletions .deco/blocks/Dark%20.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"__resolveType": "site/sections/Theme/Theme.tsx",
"buttonStyle": {
"--border-btn": "1px",
"--rounded-btn": "0.2rem",
"--btn-focus-scale": 0.95,
"--animation-btn": "0.25s"
},
"otherStyles": {
"--rounded-box": "1rem",
"--rounded-badge": "1.9rem",
"--animation-input": "0.2s",
"--tab-border": "1px",
"--tab-radius": "0.5rem"
},
"mainColors": {
"primary": "#FFFFFF",
"base-100": "#030806",
"secondary": "#02F67C",
"tertiary": "#C9CFCF"
},
"complementaryColors": { "base-200": "#162121" },
"colorScheme": "dark"
}
3 changes: 2 additions & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
"@std/testing": "jsr:@std/testing@^1.0.0",
"@zaubrik/djwt": "jsr:@zaubrik/djwt@^3.0.2",
"fast-json-patch": "npm:fast-json-patch@^3.1.1",
"simple-git": "npm:simple-git@^3.25.0"
"simple-git": "npm:simple-git@^3.25.0",
"hightlight.js": "npm:highlight.js@11.10.0"
},
"tasks": {
"start": "deno run -A --unstable-http --env https://deco.cx/run -- deno task dev",
Expand Down
2 changes: 2 additions & 0 deletions fresh.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import * as $Cms from "./islands/Cms.tsx";
import * as $ContactUs from "./islands/ContactUs.tsx";
import * as $FaviconImage from "./islands/FaviconImage.tsx";
import * as $ForwardBackButtons_ForwardBackButtons from "./islands/ForwardBackButtons/ForwardBackButtons.tsx";
import * as $Glossary_Content from "./islands/Glossary/Content.tsx";
import * as $Glossary_Search from "./islands/Glossary/Search.tsx";
import * as $Graph from "./islands/Graph.tsx";
import * as $Header from "./islands/Header.tsx";
Expand Down Expand Up @@ -97,6 +98,7 @@ const manifest = {
"./islands/FaviconImage.tsx": $FaviconImage,
"./islands/ForwardBackButtons/ForwardBackButtons.tsx":
$ForwardBackButtons_ForwardBackButtons,
"./islands/Glossary/Content.tsx": $Glossary_Content,
"./islands/Glossary/Search.tsx": $Glossary_Search,
"./islands/Graph.tsx": $Graph,
"./islands/Header.tsx": $Header,
Expand Down
46 changes: 46 additions & 0 deletions islands/Glossary/Content.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Head } from "$fresh/runtime.ts";
import hljs from "hightlight.js";
import { useEffect, useRef } from "preact/hooks";

const PARAGRAPH_STYLES = "[&_p]:leading-[150%] [&_*]:mb-4 text-[#cdcdcd]";
const HEADING_STYLES =
"[&>h1]:text-white [&>h1]:text-4xl [&>h1]:my-6 [&>h1]:font-bold [&>h2]:text-white [&>h2]:text-3xl [&>h2]:my-6 [&>h2]:font-bold [&>h1]:text-2xl [&>h1]:my-6 [&>h1]:font-bold [&>h3]:text-white [&>h3]:text-2xl [&>h3]:font-bold [&>h4]:text-xl [&>h4]:text-white [&>h4]:my-6 [&>h4]:font-bold [&>h5]:text-lg [&>h5]:text-white [&>h5]:my-6 [&>h5]:font-bold [&>h6]:text-white [&>h6]:my-6 [&>h6]:font-bold";
const CODE_BLOCK_STYLES =
"[&>pre]:bg-gray-100 [&>pre]:text-white [&_code]:mb-0 [&>pre]:text-wrap [&>code]:p-4 [&>pre]:font-mono [&>pre]:text-sm [&>pre]:border-2 [&>pre]:border-[#ffffff26] [&>pre]:rounded-md [&>pre]:overflow-x-auto [&>pre]:bg-transparent [&>code]:block [&>code]:w-full";
const IMAGE_STYLES = "[&_img]:rounded-2xl [&_img]:w-full [&_img]:my-12";
const BLOCKQUOTE_STYLES =
"[&>blockquote]:my-6 [&>blockquote]:border-l-2 [&>blockquote]:border-black [&>blockquote]:text-xl [&>blockquote]:italic [&>blockquote]:pl-6";

const CONTENT_STYLES = `code-container ${PARAGRAPH_STYLES} ${HEADING_STYLES} ${CODE_BLOCK_STYLES} ${IMAGE_STYLES} ${BLOCKQUOTE_STYLES}`;

export default function Content({ content }: { content: string }) {
const container = useRef<HTMLDivElement>(null);

useEffect(() => {
if (container.current) {
container.current.innerHTML = content;

container.current.querySelectorAll("pre code").forEach((block) => {
return hljs.highlightBlock(block);
});
}
}, [content, container]);

return (
<>
<Head>
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/highlight.js@11.8.0/styles/github-dark-dimmed.min.css"
/>
</Head>
<div
class={CONTENT_STYLES}
ref={container}
dangerouslySetInnerHTML={{
__html: content,
}}
></div>
</>
);
}
165 changes: 78 additions & 87 deletions sections/Glossary/GlossaryPost.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import Image from "apps/website/components/Image.tsx";
import type { RequestURLParam } from "apps/website/functions/requestToParam.ts";
import Icon, { AvailableIcons } from "site/components/ui/Icon.tsx";
import GlossaryItem from "site/components/Glossary/GlossaryItem.tsx";
import Content from "site/islands/Glossary/Content.tsx";
import { Head } from "$fresh/runtime.ts";

/**
* @title {{{text}}}
Expand Down Expand Up @@ -34,17 +36,6 @@ interface Props {
disclaimer?: Disclaimer;
}

const PARAGRAPH_STYLES = "[&_p]:leading-[150%] [&_*]:mb-4 text-[#cdcdcd]";
const HEADING_STYLES =
"[&>h1]:text-white [&>h1]:text-4xl [&>h1]:my-6 [&>h1]:font-bold [&>h2]:text-white [&>h2]:text-3xl [&>h2]:my-6 [&>h2]:font-bold [&>h1]:text-2xl [&>h1]:my-6 [&>h1]:font-bold [&>h3]:text-white [&>h3]:text-2xl [&>h3]:font-bold [&>h4]:text-xl [&>h4]:text-white [&>h4]:my-6 [&>h4]:font-bold [&>h5]:text-lg [&>h5]:text-white [&>h5]:my-6 [&>h5]:font-bold [&>h6]:text-white [&>h6]:my-6 [&>h6]:font-bold";
const CODE_BLOCK_STYLES =
"[&>pre]:bg-gray-100 [&>pre]:text-white [&>pre]:p-4 [&>pre]:font-mono [&>pre]:text-sm [&>pre]:border [&>pre]:rounded-md [&>pre]:overflow-x-auto [&>pre]:bg-transparent [&>code]:block [&>code]:w-full";
const IMAGE_STYLES = "[&_img]:rounded-2xl [&_img]:w-full [&_img]:my-12";
const BLOCKQUOTE_STYLES =
"[&>blockquote]:my-6 [&>blockquote]:border-l-2 [&>blockquote]:border-black [&>blockquote]:text-xl [&>blockquote]:italic [&>blockquote]:pl-6";

const CONTENT_STYLES = ` ${PARAGRAPH_STYLES} ${HEADING_STYLES} ${CODE_BLOCK_STYLES} ${IMAGE_STYLES} ${BLOCKQUOTE_STYLES}`;

const DEFAULT_AVATAR =
"https://ozksgdmyrqcxcwhnbepg.supabase.co/storage/v1/object/public/assets/1527/7286de42-e9c5-4fcb-ae8b-b992eea4b78e";

Expand Down Expand Up @@ -190,89 +181,89 @@ export default function GlossaryPost({ slug, posts, CTA, disclaimer }: Props) {
}
});

console.log(content);

return (
<div class="flex flex-col gap-12 md:gap-16 w-full container pt-28 md:pt-36 pb-16 justify-center">
<div class="md:hidden">
<div class="flex justify-between py-2 px-4 text-white">
<span class="text-lg grow">{title}</span>
<label for="glossary-menu">
<Icon
id="Menu"
size={20}
class="hover:opacity-70 transition duration-300 cursor-pointer"
/>
</label>
</div>
<input
id="glossary-menu"
name="glossary-menu"
class="peer/glossary-menu"
type="checkbox"
hidden
<>
<Head>
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/highlight.js@11.8.0/styles/github-dark-dimmed.min.css"
/>
<div class="hidden peer-checked/glossary-menu:flex w-full py-12">
<Menu posts={posts || []} currentPost={post || DEFAULT_PROPS} />
</div>
</div>
<div className="flex flex-col gap-4 items-center justify-center">
<a
href={CTA.link}
class="flex gap-2 items-center font-medium text-[#02F67C] hover:scale-105 transition duration-300"
>
<Icon id={CTA.icon} size={20} class="rotate-180" />
<span>{CTA.text}</span>
</a>
<h1 className="text-5xl text-white font-[argent-pixel]">{title}</h1>
</div>
<div class="flex flex-col lg:flex-row gap-10 lg:gap-4 justify-center">
<aside class="hidden md:flex">
<Menu posts={posts || []} currentPost={post || DEFAULT_PROPS} />
</aside>
<div className="w-full flex flex-col gap-8 container mx-auto px-4 md:px-0 lg:max-w-2xl lg:mx-0">
<div class="pl-1 bg-[#02F67C] rounded-lg">
<p class="text-white bg-[#0D1717] p-6 rounded-lg text-xl">
{excerpt}
</p>
</Head>
<div class="flex flex-col gap-12 md:gap-16 w-full container pt-28 md:pt-36 pb-16 justify-center">
<div class="md:hidden">
<div class="flex justify-between py-2 px-4 text-white">
<span class="text-lg grow">{title}</span>
<label for="glossary-menu">
<Icon
id="Menu"
size={20}
class="hover:opacity-70 transition duration-300 cursor-pointer"
/>
</label>
</div>
{image && (
<Image
className="w-full object-cover aspect-video max-h-[600px] rounded-2xl"
width={1066}
src={image || ""}
/>
)}
<div
class={CONTENT_STYLES}
dangerouslySetInnerHTML={{
__html: content,
}}
></div>

<div class="flex flex-col gap-2 w-full border border-[#162121] bg-[#0D1717] rounded-xl text-white font-semibold p-6">
<span class="text-xl">{disclaimer?.title}</span>
{disclaimer?.subtitle && (
<div
class="text-sm opacity-60"
dangerouslySetInnerHTML={{ __html: disclaimer?.subtitle }}
<input
id="glossary-menu"
name="glossary-menu"
class="peer/glossary-menu"
type="checkbox"
hidden
/>
<div class="hidden peer-checked/glossary-menu:flex w-full py-12">
<Menu posts={posts || []} currentPost={post || DEFAULT_PROPS} />
</div>
</div>
<div className="flex flex-col gap-4 items-center justify-center">
<a
href={CTA.link}
class="flex gap-2 items-center font-medium text-[#02F67C] hover:scale-105 transition duration-300"
>
<Icon id={CTA.icon} size={20} class="rotate-180" />
<span>{CTA.text}</span>
</a>
<h1 className="text-5xl text-white font-[argent-pixel]">{title}</h1>
</div>
<div class="flex flex-col lg:flex-row gap-10 lg:gap-4 justify-center">
<aside class="hidden md:flex">
<Menu posts={posts || []} currentPost={post || DEFAULT_PROPS} />
</aside>
<div className="w-full flex flex-col gap-8 container mx-auto px-4 md:px-0 lg:max-w-2xl lg:mx-0">
<div class="pl-1 bg-[#02F67C] rounded-lg">
<p class="text-white bg-[#0D1717] p-6 rounded-lg text-xl">
{excerpt}
</p>
</div>
{image && (
<Image
className="w-full object-cover aspect-video max-h-[600px] rounded-2xl"
width={1066}
src={image || ""}
/>
)}
</div>
<div class="flex justify-between w-full">
<NextPost
type="prev"
title={posts && posts[prevPost].title}
href={posts && posts[prevPost].slug}
/>
<NextPost
type="next"
title={posts && posts[nextPost].title}
href={posts && posts[nextPost].slug}
/>
<Content content={content} />
<div class="flex flex-col gap-2 w-full border border-[#162121] bg-[#0D1717] rounded-xl text-white font-semibold p-6">
<span class="text-xl">{disclaimer?.title}</span>
{disclaimer?.subtitle && (
<div
class="text-sm opacity-60"
dangerouslySetInnerHTML={{ __html: disclaimer?.subtitle }}
/>
)}
</div>
<div class="flex justify-between w-full">
<NextPost
type="prev"
title={posts && posts[prevPost].title}
href={posts && posts[prevPost].slug}
/>
<NextPost
type="next"
title={posts && posts[nextPost].title}
href={posts && posts[nextPost].slug}
/>
</div>
</div>
</div>
</div>
</div>
</>
);
}

0 comments on commit b0173f8

Please sign in to comment.