Skip to content

Commit

Permalink
feat(markdown): add grid type
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <i@innei.in>
  • Loading branch information
Innei committed Jan 13, 2024
1 parent 0d67edc commit 868dbcb
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 23 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
"js-yaml": "4.1.0",
"kbar": "0.1.0-beta.45",
"markdown-escape": "2.0.0",
"markdown-to-jsx": "npm:@innei/markdown-to-jsx@7.2.1-beta.0",
"markdown-to-jsx": "npm:@innei/markdown-to-jsx@7.2.1-beta.4",
"marked": "11.1.1",
"medium-zoom": "1.1.0",
"mermaid": "10.6.1",
Expand Down
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 7 additions & 6 deletions src/components/ui/banner/Banner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,22 @@ const iconColorMap = {
}

export const Banner: FC<{
type: 'warning' | 'error' | 'success' | 'info'
type: 'warning' | 'error' | 'success' | 'info' | 'warn'
message?: string | React.ReactNode
className?: string
children?: React.ReactNode
placement?: 'center' | 'left'
showIcon?: boolean
}> = (props) => {
const Icon = IconMap[props.type] || IconMap.info
const nextType = props.type == 'warn' ? 'warning' : props.type
const Icon = IconMap[nextType] || IconMap.info
const { placement = 'center', showIcon = true } = props
return (
<div
className={clsx(
'flex flex-col items-center gap-4 rounded-md border p-6 text-neutral-900 md:flex md:flex-row dark:bg-opacity-10 dark:text-[#c4c4c4]',
bgColorMap[props.type] || bgColorMap.info,
borderColorMap[props.type] || borderColorMap.info,
'flex flex-col items-center gap-4 rounded-md border p-6 text-neutral-900 dark:bg-opacity-10 dark:text-[#c4c4c4] md:flex md:flex-row',
bgColorMap[nextType] || bgColorMap.info,
borderColorMap[nextType] || borderColorMap.info,
placement == 'center' ? 'justify-center' : 'justify-start',
props.className,
)}
Expand All @@ -62,7 +63,7 @@ export const Banner: FC<{
<Icon
className={clsx(
`flex-shrink-0 text-3xl md:mr-2 md:self-start md:text-left`,
iconColorMap[props.type] || iconColorMap.info,
iconColorMap[nextType] || iconColorMap.info,
)}
/>
)}
Expand Down
5 changes: 5 additions & 0 deletions src/components/ui/markdown/Markdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ export interface MdProps {
as?: React.ElementType

allowsScript?: boolean

removeWrapper?: boolean
}

export const Markdown: FC<MdProps & MarkdownToJSX.Options & PropsWithChildren> =
Expand All @@ -73,6 +75,7 @@ export const Markdown: FC<MdProps & MarkdownToJSX.Options & PropsWithChildren> =
additionalParserRules,
as: As = 'div',
allowsScript = false,
removeWrapper = false,
...rest
} = props

Expand Down Expand Up @@ -277,6 +280,8 @@ export const Markdown: FC<MdProps & MarkdownToJSX.Options & PropsWithChildren> =
rest,
])

if (removeWrapper) return <Suspense>{node}</Suspense>

return (
<Suspense>
<As
Expand Down
76 changes: 76 additions & 0 deletions src/components/ui/markdown/customize.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,18 @@ $ c = \pm\sqrt{a^2 + b^2} $

## Container

### banner

```
::: warning
_here be dragons_
:::
::: banner {error}
_here be dragons_
:::
```

::: warning
_here be dragons_
:::
Expand All @@ -82,13 +94,77 @@ _here be dragons_
_here be dragons_
:::

### Gallery

```
::: gallery
https://loremflickr.com/640/480/city?1
https://loremflickr.com/640/480/city?2
https://loremflickr.com/640/480/city?3
![](https://loremflickr.com/640/480/city?4 'Image')
:::
```

::: gallery
https://loremflickr.com/640/480/city?1
https://loremflickr.com/640/480/city?2
https://loremflickr.com/640/480/city?3
![](https://loremflickr.com/640/480/city?4 'Image')
:::


### Grid

```md

::: grid {cols=3,gap=4}

Grid 1

Grid 2

Grid 3

https://loremflickr.com/640/480/city?1


https://loremflickr.com/640/480/city?2

https://loremflickr.com/640/480/city?3

![](https://loremflickr.com/640/480/city?4 'Image')

![](https://loremflickr.com/640/480/city?4 'Image')

![](https://loremflickr.com/640/480/city?4 'Image')

:::
```

::: grid {cols=3,gap=4}

Grid 1

Grid 2

Grid 3

https://loremflickr.com/640/480/city?1


https://loremflickr.com/640/480/city?2

https://loremflickr.com/640/480/city?3

![](https://loremflickr.com/640/480/city?4 'Image')

![](https://loremflickr.com/640/480/city?4 'Image')

![](https://loremflickr.com/640/480/city?4 'Image')

:::

## Rich Link

```
Expand Down
60 changes: 48 additions & 12 deletions src/components/ui/markdown/parsers/container.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'
import { blockRegex, Priority } from 'markdown-to-jsx'
import React, { Fragment } from 'react'
import { Priority } from 'markdown-to-jsx'
import type { MarkdownToJSX } from 'markdown-to-jsx'

import { Banner } from '../../banner/Banner'
Expand All @@ -19,26 +19,36 @@ const shouldCatchContainerName = [
'success',
'warning',
'note',

'grid',
].join('|')

export const ContainerRule: MarkdownToJSX.Rule = {
match: blockRegex(
new RegExp(
`^\\s*::: *(?<name>(${shouldCatchContainerName})) *({(?<params>(.*?))})? *\n(?<content>[\\s\\S]+?)\\s*::: *(?:\n *)+\n?`,
),
),
match: (source: string) => {
const result =
/^\s*::: *(?<type>.*?) *(?:{(?<params>.*?)})? *\n(?<content>[\s\S]+?)\s*::: *(?:\n *)+\n?/.exec(
source,
)

if (!result) return null

const type = result.groups!.type
if (!type || !type.match(shouldCatchContainerName)) return null
return result
},
order: Priority.MED,
parse(capture) {
const { groups } = capture
return {
...groups,
node: { ...groups },
}
},
// @ts-ignore

react(node, _, state) {
const { name, content, params } = node
const { type, params, content } = node.node

switch (name) {
console.log('container', type, params, content)
switch (type) {
case 'carousel':
case 'gallery': {
return (
Expand All @@ -55,15 +65,17 @@ export const ContainerRule: MarkdownToJSX.Rule = {
const transformMap = {
warning: 'warn',
danger: 'error',
note: 'info',
}
return (
<Banner
type={name || (transformMap as any)[name] || 'info'}
type={(transformMap as any)[type] || type}
className="my-4"
key={state?.key}
>
<Markdown
value={content}
as={Fragment}
allowsScript
className="w-full [&>p:first-child]:mt-0"
/>
Expand All @@ -85,6 +97,30 @@ export const ContainerRule: MarkdownToJSX.Rule = {
</Banner>
)
}

case 'grid': {
// cols=2,gap=4

const cols = params?.match(/cols=(?<cols>\d+)/)?.groups?.cols || 2
const gap = params?.match(/gap=(?<gap>\d+)/)?.groups?.gap || 4
return (
<div
className="grid w-full"
style={{
gridTemplateColumns: `repeat(${cols}, minmax(0, 1fr))`,
gap: `${gap}px`,
}}
key={state?.key}
>
<Markdown
value={content}
allowsScript
removeWrapper
className="w-full [&>p:first-child]:mt-0"
/>
</div>
)
}
}

return (
Expand Down

1 comment on commit 868dbcb

@vercel
Copy link

@vercel vercel bot commented on 868dbcb Jan 13, 2024

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:

shiro – ./

shiro-git-main-innei.vercel.app
springtide.vercel.app
shiro-innei.vercel.app
innei.in

Please sign in to comment.