Skip to content

Commit

Permalink
feat: support header
Browse files Browse the repository at this point in the history
  • Loading branch information
Dunqing committed May 8, 2022
1 parent 78d6cdf commit 1e13f62
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 11 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ d: ""

# 邓清

## 个人信息

- Github: [https://github.com/Dunqing](https://github.com/Dunqing)
- [https://github.com/Dunqing](https://github.com/Dunqing)
>
- dengqing0821@gmail.com
- 2.5年
- 21岁
>
- Telegram: @luckingforme
- Email: dengqing0821@gmail.com
- 工作经验:2.5年
- 年龄:21


## 个人总结

Expand Down
19 changes: 15 additions & 4 deletions playground/src/Resume.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import '@unocss/reset/tailwind.css'
import remarkFrontmatter from 'remark-frontmatter'
import remarkGfm from 'remark-gfm'
import clsx from 'clsx'
import { description, meta, table } from './plugins'
import { description, header, meta, table } from './plugins'

function Resume() {
const [markdown] = useState<string>(() => {
Expand Down Expand Up @@ -57,9 +57,19 @@ function Resume() {
return <span className="text-blue-gray-600" {...props} />
},
'description': ({ ...props }) => {
return <p {...props} className="text-sm indent">

</p>
return <p {...props} className="text-sm indent" />
},
'header': ({ node: _, ...props }) => {
return <header {...props} className="flex flex-col items-center p-4" />
},
'header-h1': ({ node: _, ...props }) => {
return <h1 {...props} className="text-2xl" />
},
'header-ul': ({ ...props }) => {
return <ul {...props} className="list-none flex flex-wrap" />
},
'header-li': ({ ...props }) => {
return <li className="sibling:before:content-| sibling:before:p-x-2 sibling:before:text-gray-400" {...props} />
},
}}
remarkPlugins={[
Expand All @@ -69,6 +79,7 @@ function Resume() {
]}
rehypePlugins={[
table,
header,
description,
]}
>
Expand Down
4 changes: 4 additions & 0 deletions playground/src/jsx.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ declare global {
'card-item-label': React.DetailedHTMLProps<React.HTMLAttributes<HTMLElement>, HTMLElement>
'card-item-value': React.DetailedHTMLProps<React.HTMLAttributes<HTMLElement>, HTMLElement>
'description': React.DetailedHTMLProps<React.HTMLAttributes<HTMLElement>, HTMLElement>
'header': React.DetailedHTMLProps<React.HTMLAttributes<HTMLElement>, HTMLElement>
'header-h1': React.DetailedHTMLProps<React.HTMLAttributes<HTMLElement>, HTMLElement>
'header-ul': React.DetailedHTMLProps<React.HTMLAttributes<HTMLElement>, HTMLElement>
'header-li': React.DetailedHTMLProps<React.HTMLAttributes<HTMLElement>, HTMLElement>
}
}
}
4 changes: 3 additions & 1 deletion playground/src/plugins/_util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { is } from 'unist-util-is'

const HeadingTag = ['h1', 'h2', 'h3']

export const isHeading = (t: any): t is Element => is(t, { type: 'element' }) && HeadingTag.includes((t as Element).tagName)
export const isHeading = (t: any, tagName?: string | string[]): t is Element => is(t, { type: 'element' }) && tagName ? [tagName].flat().includes((t as Element).tagName) : HeadingTag.includes((t as Element).tagName)
export const isTable = (t: any): t is Element => is(t, { type: 'element', tagName: 'table' }) && HeadingTag.includes((t as Element).tagName)
export const isText = (t: any): t is Text => is(t, { type: 'text' })
export const isUl = <T>(t: T): t is T => is(t, { type: 'element', tagName: 'ul' })
export const isBlockquote = (t: any) => is(t, { type: 'element', tagName: 'blockquote' })
47 changes: 47 additions & 0 deletions playground/src/plugins/header.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import type { Plugin } from 'unified'
import { visit } from 'unist-util-visit'
import type { Element } from 'hast'
import { isBlockquote, isUl } from './_util'

export const header: Plugin<[], Element> = function () {
return (root) => {
visit(root, {
type: 'element',
tagName: 'h1',
}, (paragraph, index, parent) => {
const len = parent.children.length
const children = []
const delIndex: number[] = []

for (let i = index + 2; i < len; i += 2) {
const element = parent.children[i] as Element

if (isBlockquote(element))
continue

if (!isUl(element))
break

children.push(element)

element.tagName = 'header-ul'

visit(element, { tagName: 'li' }, (li) => {
li.tagName = 'header-li'
})

delIndex.unshift(i)
}

if (children.length) {
paragraph.tagName = 'header-h1'
delIndex.forEach(i => parent.children.splice(i, 1))
parent.children.splice(index, 1, {
type: 'element',
tagName: 'header',
children: [paragraph, ...children],
})
}
})
}
}
1 change: 1 addition & 0 deletions playground/src/plugins/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './meta'
export * from './table'
export * from './description'
export * from './header'

0 comments on commit 1e13f62

Please sign in to comment.