Skip to content

Commit f3d55d9

Browse files
authored
Merge pull request #463 from 1chooo/feat/anchor-header
Feat/anchor header
2 parents 4579a9c + 1e5c5c0 commit f3d55d9

File tree

5 files changed

+79
-35
lines changed

5 files changed

+79
-35
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import React from 'react';
2+
import slugify from 'slugify';
3+
4+
interface HeaderProps {
5+
level: 1 | 2 | 3 | 4 | 5 | 6;
6+
children: React.ReactNode;
7+
}
8+
9+
const AnchorHeader: React.FC<HeaderProps> = ({ level, children, ...props }) => {
10+
const Tag = `h${level}` as keyof JSX.IntrinsicElements; // Dynamically choose the header tag
11+
const id = slugify(children?.toString() ?? '', { lower: true });
12+
13+
const getMargins = (level: number) => {
14+
switch (level) {
15+
case 1:
16+
return { marginTop: '3.0rem', marginBottom: '2.0rem' };
17+
case 2:
18+
return { marginTop: '2.5rem', marginBottom: '1.5rem' };
19+
case 3:
20+
return { marginTop: '2.0rem', marginBottom: '1.0rem' };
21+
case 4:
22+
return { marginTop: '1.5rem', marginBottom: '0.8rem' };
23+
case 5:
24+
return { marginTop: '1.2rem', marginBottom: '0.6rem' };
25+
case 6:
26+
return { marginTop: '1.0rem', marginBottom: '0.5rem' };
27+
default:
28+
return {};
29+
}
30+
};
31+
32+
const margins = getMargins(level);
33+
34+
return (
35+
<Tag id={id} className="anchor-header" style={margins} {...props}>
36+
<a href={`#${id}`}>
37+
<span className="hash">#</span>
38+
{children}
39+
</a>
40+
</Tag>
41+
);
42+
};
43+
44+
export default AnchorHeader;

apps/web/src/components/markdown/anchor.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const Anchor: React.FC<AnchorProps> = ({ children, ...props }) => {
2121

2222
return (
2323
<a
24-
className="code-highlight-yellow"
24+
className="blog-text-a"
2525
target="_blank"
2626
rel="noreferrer"
2727
{...props}

apps/web/src/components/markdown/markdown-renderer.tsx

+32-27
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import BlockQuote from './block-quote';
99
import CodeBlock from './code-block';
1010
import MarkdownImage from './markdown-image';
1111
import Paragraph from './paragraph';
12+
import AnchorHeader from './anchor-header';
1213

1314
interface MarkdownRendererProps {
1415
className?: string;
@@ -54,33 +55,37 @@ const MarkdownRenderer: React.FC<MarkdownRendererProps> = ({
5455
}}
5556
/>
5657
),
57-
h1: (props) => (
58-
<h1
59-
{...props}
60-
style={{
61-
marginTop: '3.0rem',
62-
marginBottom: '2.0rem',
63-
}}
64-
/>
65-
),
66-
h2: (props) => (
67-
<h2
68-
{...props}
69-
style={{
70-
marginTop: '2.5rem',
71-
marginBottom: '1.5rem',
72-
}}
73-
/>
74-
),
75-
h3: (props) => (
76-
<h3
77-
{...props}
78-
style={{
79-
marginTop: '2.0rem',
80-
marginBottom: '1.0rem',
81-
}}
82-
/>
83-
),
58+
// h1: (props) => (
59+
// <h1
60+
// {...props}
61+
// style={{
62+
// marginTop: '3.0rem',
63+
// marginBottom: '2.0rem',
64+
// }}
65+
// />
66+
// ),
67+
// h2: (props) => (
68+
// <h2
69+
// {...props}
70+
// style={{
71+
// marginTop: '2.5rem',
72+
// marginBottom: '1.5rem',
73+
// }}
74+
// />
75+
// ),
76+
// h3: (props) => (
77+
// <h3
78+
// {...props}
79+
// style={{
80+
// marginTop: '2.0rem',
81+
// marginBottom: '1.0rem',
82+
// }}
83+
// />
84+
// ),
85+
// Usage:
86+
h1: ({ children, ...props }) => <AnchorHeader level={1} {...props}>{children}</AnchorHeader>,
87+
h2: ({ children, ...props }) => <AnchorHeader level={2} {...props}>{children}</AnchorHeader>,
88+
h3: ({ children, ...props }) => <AnchorHeader level={3} {...props}>{children}</AnchorHeader>,
8489
blockquote: (props) => <BlockQuote {...props}>{props.children}</BlockQuote>,
8590
code({ node, inline, className, children, ...props }: any) {
8691
const match = /language-(\w+)/.exec(className || '');

apps/web/src/contents/portfolios/todam-tw.mdx

-5
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,6 @@ This is the project that I served as an Intern at eCloudValley in 2024/03-2024/0
2727

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

30-
31-
<h3 align="center">
32-
<b><i>The frontend with gradio and combined with the API endpoints for the ticket system.</i></b>
33-
</h3>
34-
3530
## Developing Requirements
3631

3732
Python version `python3.11` or later with [`poetry`](https://python-poetry.org/) to manage the dependencies.

apps/web/src/styles/blog/blog-text.css

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
margin-bottom: 15px;
99
}
1010

11-
.blog-text a {
11+
.blog-text-a {
1212
display: inline;
1313
color: var(--orange-yellow-crayola);
1414
text-decoration: underline;
1515
}
1616

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

0 commit comments

Comments
 (0)