Skip to content

Commit

Permalink
Merge pull request #463 from 1chooo/feat/anchor-header
Browse files Browse the repository at this point in the history
Feat/anchor header
  • Loading branch information
1chooo authored Nov 20, 2024
2 parents 4579a9c + 1e5c5c0 commit f3d55d9
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 35 deletions.
44 changes: 44 additions & 0 deletions apps/web/src/components/markdown/anchor-header.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import React from 'react';
import slugify from 'slugify';

interface HeaderProps {
level: 1 | 2 | 3 | 4 | 5 | 6;
children: React.ReactNode;
}

const AnchorHeader: React.FC<HeaderProps> = ({ level, children, ...props }) => {
const Tag = `h${level}` as keyof JSX.IntrinsicElements; // Dynamically choose the header tag
const id = slugify(children?.toString() ?? '', { lower: true });

const getMargins = (level: number) => {
switch (level) {
case 1:
return { marginTop: '3.0rem', marginBottom: '2.0rem' };
case 2:
return { marginTop: '2.5rem', marginBottom: '1.5rem' };
case 3:
return { marginTop: '2.0rem', marginBottom: '1.0rem' };
case 4:
return { marginTop: '1.5rem', marginBottom: '0.8rem' };
case 5:
return { marginTop: '1.2rem', marginBottom: '0.6rem' };
case 6:
return { marginTop: '1.0rem', marginBottom: '0.5rem' };
default:
return {};
}
};

const margins = getMargins(level);

return (
<Tag id={id} className="anchor-header" style={margins} {...props}>
<a href={`#${id}`}>
<span className="hash">#</span>
{children}
</a>
</Tag>
);
};

export default AnchorHeader;
2 changes: 1 addition & 1 deletion apps/web/src/components/markdown/anchor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const Anchor: React.FC<AnchorProps> = ({ children, ...props }) => {

return (
<a
className="code-highlight-yellow"
className="blog-text-a"
target="_blank"
rel="noreferrer"
{...props}
Expand Down
59 changes: 32 additions & 27 deletions apps/web/src/components/markdown/markdown-renderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import BlockQuote from './block-quote';
import CodeBlock from './code-block';
import MarkdownImage from './markdown-image';
import Paragraph from './paragraph';
import AnchorHeader from './anchor-header';

interface MarkdownRendererProps {
className?: string;
Expand Down Expand Up @@ -54,33 +55,37 @@ const MarkdownRenderer: React.FC<MarkdownRendererProps> = ({
}}
/>
),
h1: (props) => (
<h1
{...props}
style={{
marginTop: '3.0rem',
marginBottom: '2.0rem',
}}
/>
),
h2: (props) => (
<h2
{...props}
style={{
marginTop: '2.5rem',
marginBottom: '1.5rem',
}}
/>
),
h3: (props) => (
<h3
{...props}
style={{
marginTop: '2.0rem',
marginBottom: '1.0rem',
}}
/>
),
// h1: (props) => (
// <h1
// {...props}
// style={{
// marginTop: '3.0rem',
// marginBottom: '2.0rem',
// }}
// />
// ),
// h2: (props) => (
// <h2
// {...props}
// style={{
// marginTop: '2.5rem',
// marginBottom: '1.5rem',
// }}
// />
// ),
// h3: (props) => (
// <h3
// {...props}
// style={{
// marginTop: '2.0rem',
// marginBottom: '1.0rem',
// }}
// />
// ),
// Usage:
h1: ({ children, ...props }) => <AnchorHeader level={1} {...props}>{children}</AnchorHeader>,
h2: ({ children, ...props }) => <AnchorHeader level={2} {...props}>{children}</AnchorHeader>,
h3: ({ children, ...props }) => <AnchorHeader level={3} {...props}>{children}</AnchorHeader>,
blockquote: (props) => <BlockQuote {...props}>{props.children}</BlockQuote>,
code({ node, inline, className, children, ...props }: any) {
const match = /language-(\w+)/.exec(className || '');
Expand Down
5 changes: 0 additions & 5 deletions apps/web/src/contents/portfolios/todam-tw.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@ This is the project that I served as an Intern at eCloudValley in 2024/03-2024/0

![TODAM Ticket System](https://github.com/TODAM-tw/todam-ticket-system/raw/main/docs/imgs/cover.png)


<h3 align="center">
<b><i>The frontend with gradio and combined with the API endpoints for the ticket system.</i></b>
</h3>

## Developing Requirements

Python version `python3.11` or later with [`poetry`](https://python-poetry.org/) to manage the dependencies.
Expand Down
4 changes: 2 additions & 2 deletions apps/web/src/styles/blog/blog-text.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
margin-bottom: 15px;
}

.blog-text a {
.blog-text-a {
display: inline;
color: var(--orange-yellow-crayola);
text-decoration: underline;
}

.blog-text a:hover {
.blog-text-a:hover {
text-decoration-color: hsla(0, 0%, 84%, 0.7);
}

Expand Down

0 comments on commit f3d55d9

Please sign in to comment.