mirror of
https://github.com/reonokiy/blog.nokiy.net.git
synced 2025-06-15 11:12:54 +02:00
test: add plugins
This commit is contained in:
parent
4341dab741
commit
9e9cdcb206
8 changed files with 257 additions and 4 deletions
|
@ -20,6 +20,11 @@ import remarkSectionize from 'remark-sectionize'
|
|||
//
|
||||
import UnoCSS from 'unocss/astro'
|
||||
import { themeConfig } from './src/config'
|
||||
import { AdmonitionComponent } from './src/plugins/rehype-component-admonition.ts'
|
||||
import { GithubCardComponent } from './src/plugins/rehype-component-github-card.ts'
|
||||
import { parseDirectiveNode } from './src/plugins/remark-directive-rehype.ts'
|
||||
import { remarkExcerpt } from './src/plugins/remark-excerpt.ts'
|
||||
import { remarkReadingTime } from './src/plugins/remark-reading-time.ts'
|
||||
|
||||
const { url }: { url: ThemeConfig['site']['url'] } = themeConfig.site
|
||||
|
||||
|
|
|
@ -26,7 +26,6 @@
|
|||
"markdown-it": "^14.1.0",
|
||||
"overlayscrollbars": "^2.10.1",
|
||||
"photoswipe": "^5.4.4",
|
||||
"reading-time": "^1.5.0",
|
||||
"rehype-autolink-headings": "^7.1.0",
|
||||
"rehype-components": "^0.3.0",
|
||||
"rehype-external-links": "^3.0.0",
|
||||
|
@ -48,8 +47,10 @@
|
|||
"devDependencies": {
|
||||
"@antfu/eslint-config": "^3.14.0",
|
||||
"@types/markdown-it": "^14.1.2",
|
||||
"@types/mdast": "^4.0.4",
|
||||
"@types/node": "^22.10.5",
|
||||
"@types/sanitize-html": "^2.13.0",
|
||||
"@types/unist": "^3.0.3",
|
||||
"@unocss/eslint-plugin": "^65.4.0",
|
||||
"@unocss/preset-attributify": "^65.4.0",
|
||||
"@unocss/reset": "^65.4.0",
|
||||
|
@ -59,7 +60,10 @@
|
|||
"eslint": "^9.18.0",
|
||||
"eslint-plugin-astro": "^1.3.1",
|
||||
"esno": "^4.8.0",
|
||||
"hastscript": "^9.0.0",
|
||||
"lint-staged": "^15.3.0",
|
||||
"mdast-util-to-string": "^4.0.0",
|
||||
"reading-time": "^1.5.0",
|
||||
"unocss": "^65.4.0",
|
||||
"unocss-preset-theme": "^0.14.1"
|
||||
},
|
||||
|
|
21
pnpm-lock.yaml
generated
21
pnpm-lock.yaml
generated
|
@ -44,9 +44,6 @@ importers:
|
|||
photoswipe:
|
||||
specifier: ^5.4.4
|
||||
version: 5.4.4
|
||||
reading-time:
|
||||
specifier: ^1.5.0
|
||||
version: 1.5.0
|
||||
rehype-autolink-headings:
|
||||
specifier: ^7.1.0
|
||||
version: 7.1.0
|
||||
|
@ -102,15 +99,24 @@ importers:
|
|||
'@antfu/eslint-config':
|
||||
specifier: ^3.14.0
|
||||
version: 3.14.0(@typescript-eslint/utils@8.19.1(eslint@9.18.0(jiti@2.4.2))(typescript@5.7.3))(@unocss/eslint-plugin@65.4.0(eslint@9.18.0(jiti@2.4.2))(typescript@5.7.3))(@vue/compiler-sfc@3.5.13)(astro-eslint-parser@1.1.0(typescript@5.7.3))(eslint-plugin-astro@1.3.1(eslint@9.18.0(jiti@2.4.2))(typescript@5.7.3))(eslint@9.18.0(jiti@2.4.2))(typescript@5.7.3)
|
||||
'@types/hast':
|
||||
specifier: ^3.0.4
|
||||
version: 3.0.4
|
||||
'@types/markdown-it':
|
||||
specifier: ^14.1.2
|
||||
version: 14.1.2
|
||||
'@types/mdast':
|
||||
specifier: ^4.0.4
|
||||
version: 4.0.4
|
||||
'@types/node':
|
||||
specifier: ^22.10.5
|
||||
version: 22.10.5
|
||||
'@types/sanitize-html':
|
||||
specifier: ^2.13.0
|
||||
version: 2.13.0
|
||||
'@types/unist':
|
||||
specifier: ^3.0.3
|
||||
version: 3.0.3
|
||||
'@unocss/eslint-plugin':
|
||||
specifier: ^65.4.0
|
||||
version: 65.4.0(eslint@9.18.0(jiti@2.4.2))(typescript@5.7.3)
|
||||
|
@ -138,9 +144,18 @@ importers:
|
|||
esno:
|
||||
specifier: ^4.8.0
|
||||
version: 4.8.0
|
||||
hastscript:
|
||||
specifier: ^9.0.0
|
||||
version: 9.0.0
|
||||
lint-staged:
|
||||
specifier: ^15.3.0
|
||||
version: 15.3.0
|
||||
mdast-util-to-string:
|
||||
specifier: ^4.0.0
|
||||
version: 4.0.0
|
||||
reading-time:
|
||||
specifier: ^1.5.0
|
||||
version: 1.5.0
|
||||
unocss:
|
||||
specifier: ^65.4.0
|
||||
version: 65.4.0(postcss@8.4.49)(rollup@2.79.2)(vite@6.0.7(@types/node@22.10.5)(jiti@2.4.2)(lightningcss@1.28.2)(terser@5.37.0)(tsx@4.19.2)(yaml@2.7.0))(vue@3.5.13(typescript@5.7.3))
|
||||
|
|
50
src/plugins/rehype-component-admonition.ts
Normal file
50
src/plugins/rehype-component-admonition.ts
Normal file
|
@ -0,0 +1,50 @@
|
|||
import type { Element, Properties as HastProperties, Node } from 'hast'
|
||||
/// <reference types="mdast" />
|
||||
import { h } from 'hastscript'
|
||||
|
||||
interface AdmonitionProperties extends HastProperties {
|
||||
'title'?: string
|
||||
'has-directive-label'?: boolean
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an admonition component.
|
||||
*
|
||||
* @param properties - The properties of the component.
|
||||
* @param type - The admonition type.
|
||||
* @param children - The children elements of the component.
|
||||
* @returns The created admonition component as a Hast Element.
|
||||
*/
|
||||
export function AdmonitionComponent(
|
||||
properties: AdmonitionProperties,
|
||||
type: 'tip' | 'note' | 'important' | 'caution' | 'warning',
|
||||
children: Node[],
|
||||
): Element {
|
||||
if (!Array.isArray(children) || children.length === 0) {
|
||||
return h(
|
||||
'div',
|
||||
{ class: 'hidden' },
|
||||
'Invalid admonition directive. (Admonition directives must be of block type ":::note{name="name"} <content> :::")',
|
||||
)
|
||||
}
|
||||
|
||||
let label: Element | string | null = null
|
||||
|
||||
if (properties && properties['has-directive-label']) {
|
||||
const [firstChild, ...restChildren] = children
|
||||
children = restChildren
|
||||
|
||||
if (firstChild && firstChild.type === 'element') {
|
||||
label = firstChild as Element
|
||||
label.tagName = 'div' // Change the tag <p> to <div>
|
||||
}
|
||||
else {
|
||||
label = ''
|
||||
}
|
||||
}
|
||||
|
||||
return h('blockquote', { class: `admonition bdm-${type}` }, [
|
||||
h('span', { class: 'bdm-title' }, label || type.toUpperCase()),
|
||||
...(children as Element[]),
|
||||
] as Element[])
|
||||
}
|
108
src/plugins/rehype-component-github-card.ts
Normal file
108
src/plugins/rehype-component-github-card.ts
Normal file
|
@ -0,0 +1,108 @@
|
|||
/// <reference types="mdast" />
|
||||
import { h } from 'hastscript'
|
||||
|
||||
/**
|
||||
* Creates a GitHub Card component.
|
||||
*
|
||||
* @param {object} properties - The properties of the component.
|
||||
* @param {string} properties.repo - The GitHub repository in the format "owner/repo".
|
||||
* @param {import('mdast').RootContent[]} children - The children elements of the component.
|
||||
* @returns {import('mdast').Parent} The created GitHub Card component.
|
||||
*/
|
||||
export function GithubCardComponent(
|
||||
properties: { repo: string },
|
||||
children: import('mdast').RootContent[],
|
||||
): import('mdast').Parent {
|
||||
if (Array.isArray(children) && children.length !== 0) {
|
||||
return h('div', { class: 'hidden' }, [
|
||||
'Invalid directive. ("github" directive must be leaf type "::github{repo="owner/repo"}")',
|
||||
]) as unknown as import('mdast').Parent
|
||||
}
|
||||
|
||||
if (!properties.repo || !properties.repo.includes('/')) {
|
||||
return h(
|
||||
'div',
|
||||
{ class: 'hidden' },
|
||||
'Invalid repository. ("repo" attributte must be in the format "owner/repo")',
|
||||
) as unknown as import('mdast').Parent
|
||||
}
|
||||
|
||||
const repo = properties.repo
|
||||
const cardUuid = `GC${Math.random().toString(36).slice(-6)}` // Collisions are not important
|
||||
|
||||
const nAvatar = h(`div#${cardUuid}-avatar`, { class: 'gc-avatar' })
|
||||
const nLanguage = h(
|
||||
`span#${cardUuid}-language`,
|
||||
{ class: 'gc-language' },
|
||||
'Waiting...',
|
||||
)
|
||||
|
||||
const nTitle = h(`div`, { class: 'gc-titlebar' }, [
|
||||
h('div', { class: 'gc-titlebar-left' }, [
|
||||
h('div', { class: 'gc-owner' }, [
|
||||
nAvatar,
|
||||
h('div', { class: 'gc-user' }, repo.split('/')[0]),
|
||||
]),
|
||||
h('div', { class: 'gc-divider' }, '/'),
|
||||
h('div', { class: 'gc-repo' }, repo.split('/')[1]),
|
||||
]),
|
||||
h('div', { class: 'github-logo' }),
|
||||
])
|
||||
|
||||
const nDescription = h(
|
||||
`div#${cardUuid}-description`,
|
||||
{ class: 'gc-description' },
|
||||
'Waiting for api.github.com...',
|
||||
)
|
||||
|
||||
const nStars = h(`div#${cardUuid}-stars`, { class: 'gc-stars' }, '00K')
|
||||
const nForks = h(`div#${cardUuid}-forks`, { class: 'gc-forks' }, '0K')
|
||||
const nLicense = h(`div#${cardUuid}-license`, { class: 'gc-license' }, '0K')
|
||||
|
||||
const nScript = h(
|
||||
`script#${cardUuid}-script`,
|
||||
{ type: 'text/javascript', defer: true },
|
||||
`
|
||||
fetch('https://api.github.com/repos/${repo}', { referrerPolicy: "no-referrer" }).then(response => response.json()).then(data => {
|
||||
if (data.description) {
|
||||
document.getElementById('${cardUuid}-description').innerText = data.description.replace(/:[a-zA-Z0-9_]+:/g, '');
|
||||
} else {
|
||||
document.getElementById('${cardUuid}-description').innerText = "Description not set"
|
||||
}
|
||||
document.getElementById('${cardUuid}-language').innerText = data.language;
|
||||
document.getElementById('${cardUuid}-forks').innerText = Intl.NumberFormat('en-us', { notation: "compact", maximumFractionDigits: 1 }).format(data.forks).replaceAll("\\u202F", '');
|
||||
document.getElementById('${cardUuid}-stars').innerText = Intl.NumberFormat('en-us', { notation: "compact", maximumFractionDigits: 1 }).format(data.stargazers_count).replaceAll("\\u202F", '');
|
||||
const avatarEl = document.getElementById('${cardUuid}-avatar');
|
||||
avatarEl.style.backgroundImage = 'url(' + data.owner.avatar_url + ')';
|
||||
avatarEl.style.backgroundColor = 'transparent';
|
||||
if (data.license?.spdx_id) {
|
||||
document.getElementById('${cardUuid}-license').innerText = data.license?.spdx_id
|
||||
} else {
|
||||
document.getElementById('${cardUuid}-license').innerText = "no-license"
|
||||
};
|
||||
document.getElementById('${cardUuid}-card').classList.remove("fetch-waiting");
|
||||
console.log("[GITHUB-CARD] Loaded card for ${repo} | ${cardUuid}.")
|
||||
}).catch(err => {
|
||||
const c = document.getElementById('${cardUuid}-card');
|
||||
c.classList.add("fetch-error");
|
||||
console.warn("[GITHUB-CARD] (Error) Loading card for ${repo} | ${cardUuid}.")
|
||||
})
|
||||
`,
|
||||
)
|
||||
|
||||
return h(
|
||||
`a#${cardUuid}-card`,
|
||||
{
|
||||
class: 'card-github fetch-waiting no-styling',
|
||||
href: `https://github.com/${repo}`,
|
||||
target: '_blank',
|
||||
repo,
|
||||
},
|
||||
[
|
||||
nTitle,
|
||||
nDescription,
|
||||
h('div', { class: 'gc-infobar' }, [nStars, nForks, nLicense, nLanguage]),
|
||||
nScript,
|
||||
],
|
||||
) as unknown as import('mdast').Parent
|
||||
}
|
44
src/plugins/remark-directive-rehype.ts
Normal file
44
src/plugins/remark-directive-rehype.ts
Normal file
|
@ -0,0 +1,44 @@
|
|||
import type { Node } from 'unist'
|
||||
import { h } from 'hastscript'
|
||||
import { visit } from 'unist-util-visit'
|
||||
|
||||
interface DirectiveNode extends Node {
|
||||
type: 'containerDirective' | 'leafDirective' | 'textDirective'
|
||||
name: string
|
||||
attributes?: Record<string, any>
|
||||
children: Array<{
|
||||
data?: {
|
||||
directiveLabel?: boolean
|
||||
}
|
||||
}>
|
||||
data?: {
|
||||
hName?: string
|
||||
hProperties?: Record<string, any>
|
||||
}
|
||||
}
|
||||
|
||||
export function parseDirectiveNode() {
|
||||
return (tree: Node) => {
|
||||
visit(tree, (node: unknown) => {
|
||||
const directiveNode = node as DirectiveNode
|
||||
if (
|
||||
directiveNode.type === 'containerDirective'
|
||||
|| directiveNode.type === 'leafDirective'
|
||||
|| directiveNode.type === 'textDirective'
|
||||
) {
|
||||
const data = directiveNode.data || (directiveNode.data = {})
|
||||
directiveNode.attributes = directiveNode.attributes || {}
|
||||
if (
|
||||
directiveNode.children.length > 0
|
||||
&& directiveNode.children[0].data?.directiveLabel
|
||||
) {
|
||||
directiveNode.attributes['has-directive-label'] = true
|
||||
}
|
||||
const hast = h(directiveNode.name, directiveNode.attributes)
|
||||
|
||||
data.hName = hast.tagName
|
||||
data.hProperties = hast.properties
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
26
src/plugins/remark-excerpt.ts
Normal file
26
src/plugins/remark-excerpt.ts
Normal file
|
@ -0,0 +1,26 @@
|
|||
import type { Root } from 'mdast'
|
||||
import { toString } from 'mdast-util-to-string'
|
||||
|
||||
interface AstroData {
|
||||
data: {
|
||||
astro: {
|
||||
frontmatter: {
|
||||
excerpt: string
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function remarkExcerpt() {
|
||||
return (tree: Root, file: AstroData) => {
|
||||
let excerpt = ''
|
||||
for (const node of tree.children) {
|
||||
if (node.type !== 'paragraph') {
|
||||
continue
|
||||
}
|
||||
excerpt = toString(node)
|
||||
break
|
||||
}
|
||||
file.data.astro.frontmatter.excerpt = excerpt
|
||||
}
|
||||
}
|
1
src/plugins/remark-reading-time.ts
Normal file
1
src/plugins/remark-reading-time.ts
Normal file
|
@ -0,0 +1 @@
|
|||
|
Loading…
Add table
Add a link
Reference in a new issue