Skip to content
This repository has been archived by the owner on Oct 20, 2022. It is now read-only.

283 - Add scroll anchors to h2 and h3 for mdx based pages #305

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions components/H2.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { getAnchorFromText } from "lib/clientsideUtils";

export default function H2({ children }: { children: string }) {
const anchor = getAnchorFromText(children);
const link = `#${anchor}`;
return (
<h2 id={anchor}>
{ children }
<a href={link}><div /></a>
</h2>
);
}
12 changes: 12 additions & 0 deletions components/H3.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { getAnchorFromText } from "lib/clientsideUtils";

export default function H3({ children }: { children: string }) {
const anchor = getAnchorFromText(children);
const link = `#${anchor}`;
return (
<h3 id={anchor}>
{ children }
<a href={link}><div /></a>
</h3>
);
}
30 changes: 30 additions & 0 deletions components/markdown.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,21 @@
@apply text-2xl sm:text-3xl mb-4 font-semibold;
}

.markdown h2 a div {
margin-left: 10px;
opacity: 0;
background: url('/img/anchor.svg') center no-repeat;
min-width: 20px;
min-height: 20px;
color: white;
display: inline-block;
}

.markdown h2:hover a div {
opacity: 1;
cursor: pointer;
}

.markdown h2::before {
content: "";
display: block;
Expand All @@ -22,6 +37,21 @@
@apply text-xl sm:text-2xl mb-4 font-semibold;
}

.markdown h3 a div {
margin-left: 10px;
opacity: 0;
background: url('/img/anchor.svg') center no-repeat;
min-width: 20px;
min-height: 20px;
color: white;
display: inline-block;
}

.markdown h2:hover a div {
opacity: 1;
cursor: pointer;
}

.markdown h3::before {
content: "";
display: block;
Expand Down
7 changes: 7 additions & 0 deletions lib/clientsideUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const getAnchorFromText = (text: string): string => {
return text
.toString()
.toLowerCase()
.replace(/[^a-z0-9 ]/g, '')
.replace(/[ ]/g, '-');
}
2 changes: 2 additions & 0 deletions lib/mdx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ export const components = {
Swagger: dynamic(() => import("components/Swagger")),
pre: dynamic(() => import("components/Pre")),
code: dynamic(() => import("components/Code")),
h2: dynamic(() => import("components/H2")),
h3: dynamic(() => import("components/H3")),
};
2 changes: 1 addition & 1 deletion pages/components/[...slug].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export const getStaticProps: GetStaticProps<StaticPageProps> = async ({
// Optionally pass remark/rehype plugins
mdxOptions: {
// @ts-ignore
remarkPlugins: [remarkSlug, remarkAutolinkHeadings],
remarkPlugins: [remarkSlug],
rehypePlugins: [],
},
scope: data,
Expand Down
2 changes: 1 addition & 1 deletion pages/guides/[...slug].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export const getStaticProps: GetStaticProps<StaticPageProps> = async ({
// Optionally pass remark/rehype plugins
mdxOptions: {
// @ts-ignore
remarkPlugins: [remarkSlug, remarkAutolinkHeadings],
remarkPlugins: [remarkSlug],
rehypePlugins: [],
},
scope: data,
Expand Down
2 changes: 1 addition & 1 deletion pages/overview/[...slug].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export const getStaticProps: GetStaticProps<StaticPageProps> = async ({
// Optionally pass remark/rehype plugins
mdxOptions: {
// @ts-ignore
remarkPlugins: [remarkSlug, remarkAutolinkHeadings],
remarkPlugins: [remarkSlug],
rehypePlugins: [],
},
scope: data,
Expand Down
2 changes: 1 addition & 1 deletion pages/pipeline_nodes/[...slug].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export const getStaticProps: GetStaticProps<StaticPageProps> = async ({
// Optionally pass remark/rehype plugins
mdxOptions: {
// @ts-ignore
remarkPlugins: [remarkSlug, remarkAutolinkHeadings],
remarkPlugins: [remarkSlug],
rehypePlugins: [],
},
scope: data,
Expand Down
2 changes: 1 addition & 1 deletion pages/usage/[...slug].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export const getStaticProps: GetStaticProps<StaticPageProps> = async ({
// Optionally pass remark/rehype plugins
mdxOptions: {
// @ts-ignore
remarkPlugins: [remarkSlug, remarkAutolinkHeadings],
remarkPlugins: [remarkSlug],
rehypePlugins: [],
},
scope: data,
Expand Down
1 change: 1 addition & 0 deletions public/img/anchor.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.