-
-
Notifications
You must be signed in to change notification settings - Fork 772
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Innei <[email protected]>
- Loading branch information
Showing
34 changed files
with
1,554 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import React from 'react' | ||
import { clsx } from 'clsx' | ||
import type { FC } from 'react' | ||
|
||
import { | ||
ClaritySuccessLine, | ||
FluentShieldError20Regular, | ||
FluentWarning28Regular, | ||
IonInformation, | ||
} from '../../icons/status' | ||
|
||
const IconMap = { | ||
warning: FluentWarning28Regular, | ||
info: IonInformation, | ||
error: FluentShieldError20Regular, | ||
success: ClaritySuccessLine, | ||
} | ||
|
||
const bgColorMap = { | ||
warning: 'bg-amber-50 dark:bg-amber-300', | ||
info: 'bg-always-blue-50 dark:bg-always-blue-300', | ||
success: 'bg-always-green-50 dark:bg-always-green-300', | ||
error: 'bg-always-red-50 dark:bg-always-red-300', | ||
} | ||
|
||
const borderColorMap = { | ||
warning: 'border-amber-300', | ||
info: 'border-always-blue-300', | ||
|
||
success: 'border-always-green-300', | ||
error: 'border-always-red-300', | ||
} | ||
|
||
const iconColorMap = { | ||
warning: 'text-amber-500', | ||
info: 'text-always-blue-500', | ||
success: 'text-always-green-500', | ||
error: 'text-always-red-500', | ||
} | ||
|
||
export const Banner: FC<{ | ||
type: 'warning' | 'error' | 'success' | 'info' | ||
message?: string | React.ReactNode | ||
className?: string | ||
children?: React.ReactNode | ||
placement?: 'center' | 'left' | ||
showIcon?: boolean | ||
}> = (props) => { | ||
const Icon = IconMap[props.type] || IconMap.info | ||
const { placement = 'center', showIcon = true } = props | ||
return ( | ||
<div | ||
className={clsx( | ||
'block items-center space-x-4 rounded-md border p-6 text-neutral-900 dark:bg-opacity-10 dark:text-[#c4c4c4] md:flex ' + | ||
`${bgColorMap[props.type] || bgColorMap.info} ${ | ||
borderColorMap[props.type] || borderColorMap.info | ||
}`, | ||
placement == 'center' ? 'justify-center' : 'justify-start', | ||
props.className, | ||
)} | ||
> | ||
{showIcon && ( | ||
<Icon | ||
className={`flex-shrink-0 self-start text-3xl ${ | ||
iconColorMap[props.type] || iconColorMap.info | ||
} float-left -mr-2 md:float-none md:mr-2`} | ||
/> | ||
)} | ||
{props.message ? ( | ||
<span className="leading-[1.8]">{props.message}</span> | ||
) : ( | ||
props.children | ||
)} | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './Banner' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
'use client' | ||
|
||
import { AnimatePresence, motion } from 'framer-motion' | ||
import * as React from 'react' | ||
|
||
import { microReboundPreset } from '~/constants/spring' | ||
|
||
export const Collapse = ({ | ||
isOpened, | ||
className, | ||
children, | ||
}: React.PropsWithChildren<{ isOpened: boolean } & { className?: string }>) => { | ||
// By using `AnimatePresence` to mount and unmount the contents, we can animate | ||
// them in and out while also only rendering the contents of open accordions | ||
return ( | ||
<> | ||
<AnimatePresence initial={false}> | ||
{isOpened && ( | ||
<motion.div | ||
key="content" | ||
initial="collapsed" | ||
animate="open" | ||
exit="collapsed" | ||
variants={{ | ||
open: { | ||
opacity: 1, | ||
height: 'auto', | ||
transition: microReboundPreset, | ||
}, | ||
collapsed: { | ||
opacity: 0, | ||
height: 0, | ||
overflow: 'hidden', | ||
}, | ||
}} | ||
className={className} | ||
> | ||
{children} | ||
</motion.div> | ||
)} | ||
</AnimatePresence> | ||
</> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './Collapse' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
/* eslint-disable react-hooks/rules-of-hooks */ | ||
import React, { | ||
createElement, | ||
memo, | ||
useEffect, | ||
useMemo, | ||
useRef, | ||
useState, | ||
} from 'react' | ||
import { clsx } from 'clsx' | ||
import { compiler } from 'markdown-to-jsx' | ||
import type { MarkdownToJSX } from 'markdown-to-jsx' | ||
import type { FC, PropsWithChildren } from 'react' | ||
|
||
import { range } from '~/lib/_' | ||
|
||
import styles from './index.module.css' | ||
import { CommentAtRule } from './parsers/comment-at' | ||
import { ContainerRule } from './parsers/container' | ||
import { InsertRule } from './parsers/ins' | ||
import { KateXRule } from './parsers/katex' | ||
import { MarkRule } from './parsers/mark' | ||
import { MentionRule } from './parsers/mention' | ||
import { SpoilderRule } from './parsers/spoiler' | ||
import { MParagraph, MTableBody, MTableHead, MTableRow } from './renderers' | ||
import { MDetails } from './renderers/collapse' | ||
import { MFootNote } from './renderers/footnotes' | ||
|
||
export interface MdProps { | ||
value?: string | ||
|
||
style?: React.CSSProperties | ||
readonly renderers?: { [key: string]: Partial<MarkdownToJSX.Rule> } | ||
wrapperProps?: React.DetailedHTMLProps< | ||
React.HTMLAttributes<HTMLDivElement>, | ||
HTMLDivElement | ||
> | ||
codeBlockFully?: boolean | ||
className?: string | ||
tocSlot?: (props: { headings: HTMLElement[] }) => JSX.Element | null | ||
} | ||
|
||
export const Markdown: FC<MdProps & MarkdownToJSX.Options & PropsWithChildren> = | ||
memo((props) => { | ||
const { | ||
value, | ||
renderers, | ||
style, | ||
wrapperProps = {}, | ||
codeBlockFully = false, | ||
className, | ||
overrides, | ||
extendsRules, | ||
additionalParserRules, | ||
|
||
...rest | ||
} = props | ||
|
||
const ref = useRef<HTMLDivElement>(null) | ||
const [headings, setHeadings] = useState<HTMLElement[]>([]) | ||
|
||
useEffect(() => { | ||
if (!ref.current) { | ||
return | ||
} | ||
|
||
const $headings = ref.current.querySelectorAll( | ||
range(1, 6) | ||
.map((i) => `h${i}`) | ||
.join(','), | ||
) as NodeListOf<HTMLHeadingElement> | ||
|
||
setHeadings(Array.from($headings)) | ||
|
||
return () => { | ||
setHeadings([]) | ||
} | ||
}, [value, props.children]) | ||
|
||
const node = useMemo(() => { | ||
if (!value && typeof props.children != 'string') return null | ||
|
||
const mdElement = compiler(`${value || props.children}`, { | ||
wrapper: null, | ||
// @ts-ignore | ||
overrides: { | ||
p: MParagraph, | ||
|
||
thead: MTableHead, | ||
tr: MTableRow, | ||
tbody: MTableBody, | ||
// FIXME: footer tag in raw html will renders not as expected, but footer tag in this markdown lib will wrapper as linkReferer footnotes | ||
footer: MFootNote, | ||
details: MDetails, | ||
|
||
// for custom react component | ||
// LinkCard, | ||
...overrides, | ||
}, | ||
|
||
extendsRules: { | ||
gfmTask: { | ||
react(node, _, state) { | ||
return ( | ||
<label | ||
className="mr-2 inline-flex items-center" | ||
key={state?.key} | ||
> | ||
<input type="checkbox" checked={node.completed} readOnly /> | ||
</label> | ||
) | ||
}, | ||
}, | ||
|
||
list: { | ||
react(node, output, state) { | ||
const Tag = node.ordered ? 'ol' : 'ul' | ||
|
||
return ( | ||
<Tag key={state?.key} start={node.start}> | ||
{node.items.map((item: any, i: number) => { | ||
let className = '' | ||
if (item[0]?.type == 'gfmTask') { | ||
className = 'list-none flex items-center' | ||
} | ||
|
||
return ( | ||
<li className={className} key={i}> | ||
{output(item, state!)} | ||
</li> | ||
) | ||
})} | ||
</Tag> | ||
) | ||
}, | ||
}, | ||
|
||
...extendsRules, | ||
...renderers, | ||
}, | ||
additionalParserRules: { | ||
spoilder: SpoilderRule, | ||
mention: MentionRule, | ||
commentAt: CommentAtRule, | ||
mark: MarkRule, | ||
ins: InsertRule, | ||
kateX: KateXRule, | ||
container: ContainerRule, | ||
...additionalParserRules, | ||
}, | ||
...rest, | ||
}) | ||
|
||
return mdElement | ||
}, [ | ||
value, | ||
props.children, | ||
overrides, | ||
extendsRules, | ||
renderers, | ||
additionalParserRules, | ||
rest, | ||
]) | ||
|
||
return ( | ||
<div | ||
id="write" | ||
style={style} | ||
{...wrapperProps} | ||
ref={ref} | ||
className={clsx( | ||
styles['md'], | ||
codeBlockFully ? styles['code-fully'] : undefined, | ||
wrapperProps.className, | ||
)} | ||
> | ||
{className ? <div className={className}>{node}</div> : node} | ||
|
||
{props.tocSlot ? createElement(props.tocSlot, { headings }) : null} | ||
</div> | ||
) | ||
}) |
Oops, something went wrong.
fcbbb86
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
springtide – ./
springtide.vercel.app
springtide-innei.vercel.app
springtide-git-main-innei.vercel.app