-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#314]: Blog add TOC and author for post
- Loading branch information
Showing
19 changed files
with
1,004 additions
and
312 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
--- | ||
username: 'themezv' | ||
fullName: 'Artem Zverev' | ||
avatarFileName: 'themezv.jpg' | ||
title: 'Software Engineer' | ||
--- |
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,19 @@ | ||
import Image from 'next/image' | ||
|
||
interface PostAuthorProps { | ||
avatarUrl: string | ||
username: string | ||
fullName?: string | ||
title?: string | ||
} | ||
export function PostAuthor({ username, fullName, avatarUrl, title }: PostAuthorProps) { | ||
return ( | ||
<div className="rounded-lg shadow p-4 w-full h-20 flex space-x-4"> | ||
<Image className="rounded-full w-12 h-12" width={48} height={48} src={avatarUrl} alt={fullName || username} /> | ||
<div className="overflow-hidden"> | ||
<span>{fullName || username}</span> <br /> | ||
<span className="font-light text-xs">{title}</span> | ||
</div> | ||
</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 { PostAuthor } from './PostAuthor' |
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,38 @@ | ||
import clsx from 'clsx' | ||
import type { TOCTree, TOCTreeItem } from '../../types' | ||
import { useTranslation } from '../../i18n' | ||
import type { Language } from '../../i18n/i18n.settings' | ||
|
||
interface TOCProps { | ||
toc: TOCTree | ||
locale: Language | ||
} | ||
export async function TOC({ toc, locale }: TOCProps) { | ||
const { t } = await useTranslation(locale, 'post') | ||
|
||
return ( | ||
<nav className="rounded-lg shadow p-6 w-full"> | ||
<header className="font-semibold text-gray-700">{t('tocHeader')}</header> | ||
<ul className="list-inside list-dash font-light text-gray-700 mt-2 marker:tracking-listDash"> | ||
{toc.map(heading => ( | ||
<HeadingsTree key={heading.value} item={heading} isRoot /> | ||
))} | ||
</ul> | ||
</nav> | ||
) | ||
} | ||
|
||
function HeadingsTree({ item, isRoot }: { item: TOCTreeItem; isRoot?: boolean }) { | ||
return ( | ||
<li className={clsx({ 'pl-6': !isRoot })}> | ||
<a className="hover:underline" href={`#${item.slug}`}> | ||
{item.value} | ||
</a> | ||
{item.children?.map(itemChildren => ( | ||
<ul key={itemChildren.value} className="list-inside list-dash"> | ||
<HeadingsTree item={itemChildren} isRoot={false} /> | ||
</ul> | ||
)) ?? null} | ||
</li> | ||
) | ||
} |
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 { TOC } from './TOC' |
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,27 @@ | ||
import { visit } from 'unist-util-visit' | ||
|
||
const headingsSet = new Set(['h2', 'h3', 'h4', 'h5']) | ||
|
||
export function addTOCRehypePlugin() { | ||
/** | ||
* Visit heading elements and collect them to array | ||
* | ||
* @param {import('unist').Node<import('unist').Data>} tree | ||
* @param {{data: {rawDocumentData: { headings: import('../types').HeadingsItem[] }}}} file | ||
* @returns void | ||
*/ | ||
return (tree, vFile) => { | ||
vFile.data.rawDocumentData.headings = [] | ||
visit( | ||
tree, | ||
element => headingsSet.has(element.tagName), | ||
node => { | ||
vFile.data.rawDocumentData.headings.push({ | ||
level: +node.tagName[1], | ||
value: node.children[0]?.children[0]?.value, | ||
slug: node.properties.id, | ||
}) | ||
}, | ||
) | ||
} | ||
} |
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,37 @@ | ||
// @ts-check | ||
|
||
/** | ||
* Map headings array to TOC tree | ||
* | ||
* @param {import('../types').HeadingsItem[]} headings | ||
* @param {import('../types').TOCTree} tree | ||
* @param currentLevel | ||
* @returns {import('../types').TOCTree} TOC tree | ||
* | ||
* @example | ||
* ```js | ||
* mapHeadingsToTOC([{ level: 2, value: 'Заголовок верхнего уровня' }, { level: 3, value: 'Вложенный заголовок'}, { level: 2, value: 'Заголовок верхнего уровня 2' }, { level: 3, value: 'Вложенный заголовок 1' }, {level: 4, value: 'Вложенный заголовок во вложенный заголовок 1'}]) | ||
* // [{level: 2, value: 'Заголовок верхнего уровня', children: [{level: 3, value: 'Вложенный заголовок'}]}, {level: 2, value: 'Заголовок верхнего уровня 2', children: [{depth: 3, value: 'Вложенный заголовок 1', children: [{depth: 4, value: 'Вложенный заголовок во вложенный заголовок 1'}] }]}] | ||
* ``` | ||
*/ | ||
export function mapHeadingsToTOC(headings, tree = [], currentLevel = 2) { | ||
while (headings.length > 0) { | ||
/** | ||
* @type {import('../types').TOCTreeItem} | ||
*/ | ||
const heading = headings[0] | ||
if (heading.level < currentLevel) { | ||
return tree | ||
} | ||
heading.children = [] | ||
if (heading.level === currentLevel) { | ||
headings.shift() | ||
tree.push({ ...heading, children: mapHeadingsToTOC(headings, heading.children, currentLevel + 1) }) | ||
} | ||
if (heading.level > currentLevel) { | ||
mapHeadingsToTOC(headings, heading.children, currentLevel + 1) | ||
} | ||
} | ||
|
||
return tree | ||
} |
Oops, something went wrong.