Skip to content

Commit

Permalink
chore: playground
Browse files Browse the repository at this point in the history
kricsleo committed Apr 29, 2024
1 parent 319c41e commit 8cade78
Showing 28 changed files with 16,120 additions and 7,797 deletions.
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -2,7 +2,6 @@
"name": "unplugin-markdown-2-html",
"type": "module",
"version": "0.3.4",
"packageManager": "pnpm@7.1.1",
"description": "Render markdown into html at build time.",
"license": "MIT",
"homepage": "https://github.com/kricsleo/unplugin-markdown-2-html#readme",
@@ -118,7 +117,6 @@
"eslint": "^8.29.0",
"esno": "^0.16.3",
"fast-glob": "^3.2.12",
"nodemon": "^2.0.20",
"rimraf": "^3.0.2",
"rollup": "^3.7.3",
"tsup": "^6.5.0",
9 changes: 0 additions & 9 deletions playground/.editorconfig

This file was deleted.

27 changes: 21 additions & 6 deletions playground/.gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
.DS_Store
.vite-ssg-dist
.vite-ssg-temp
*.local
# Nuxt dev/build outputs
.output
.data
.nuxt
.nitro
.cache
dist
dist-ssr

# Node dependencies
node_modules
.idea/

# Logs
logs
*.log

# Misc
.DS_Store
.fleet
.idea

# Local env files
.env
.env.*
!.env.example
1 change: 1 addition & 0 deletions playground/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
shamefully-hoist=true
21 changes: 0 additions & 21 deletions playground/LICENSE

This file was deleted.

1 change: 1 addition & 0 deletions playground/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1 align="center">Nuxt 3 Minimal Starter</h1>
26 changes: 26 additions & 0 deletions playground/app.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<script setup lang="ts">
import { css, html } from './code.md'
</script>

<template>
<main>
<article v-html="html" />
<component is="style">{{ css }}</component>
</main>
</template>

<style>
* {
margin: 0;
padding: 0;
}
html {
color-scheme: dark;
}
.shiki {
min-height: 100vh;
width: 100vw;
}
</style>
130 changes: 0 additions & 130 deletions playground/auto-imports.d.ts

This file was deleted.

140 changes: 140 additions & 0 deletions playground/code.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
```ts
import { mdAt, mdComment, mdInfo } from '@mihoyo/wave-opensdk'
import type { Article, AtNode, BlockNode, BlockquoteNode, DocNode, HeadingNode, InlineNode, LinkTextNode, ListNode, News, ParagraphNode, TableNode, TagNode, TextNode } from './types'

export function parseArticleLink(link: string) {
// 'https://km.mihoyo.com/articleBase/21832/823948'
// => { knowledgeId: 21832, articleId: 823948 }
const reg = /https:\/\/km\.mihoyo\.com\/articleBase\/(\d+)\/(\d+)/
const [, knowledgeId, articleId] = link.match(reg) || []
return { knowledgeId: +knowledgeId, articleId: +articleId }
}

export function parseArticle(article: Article) {
const doc = JSON.parse(article.content) as DocNode
const newsList = doc.content.filter(t => t.type === 'list') as ListNode[]
return newsList.map(news => parseNews(news))
}

export function parseNews(news: ListNode): News {
const titleNode = news.content.find(t => t.type === 'heading') as HeadingNode
const metaTableNode = news.content.find(t => t.type === 'table') as TableNode
const sumaryNode = news.content.find(t => t.type === 'blockquote') as BlockquoteNode
const extraLinkNodes = news.content.filter(t => t.type === 'list') as ListNode[]

const meta = parseMetaTable(metaTableNode)
const title = heading2md(titleNode)
const anchor = `st=${encodeURIComponent(heading2text(titleNode))}#${titleNode.attrs['node-id']}`
const summary = blockquote2md(sumaryNode)
const extraLinks = extraLinkNodes.map(listNode => {
const linkNode = listNode.content[0].content[0] as LinkTextNode
return parseLinkNode(linkNode)
})

return { ...meta, title, anchor, summary, extraLinks }
}

function parseMetaTable(metaTable: TableNode) {
const [_titleRow, valueRow] = metaTable.content
const [authorCell, dateCell, tagsCell, linkCell] = valueRow.content

const authorNode = authorCell.content[0].content.find(
t => t.type === 'at',
) as AtNode
const dateNode = dateCell.content[0].content.find(
t => t.type === 'text',
) as TextNode
const tagNodes = tagsCell.content[0].content.filter(
t => t.type === 'colorfulTag',
) as TagNode[]
const linkNode = linkCell.content[0].content.find(
t => t.type === 'text' && t.marks?.some(mark => mark.type === 'link'),
) as LinkTextNode

const author = authorNode.attrs.id
const date = dateNode.text
const tags = tagNodes.map(tag => tag.attrs['txt-content'])
const link = parseLinkNode(linkNode)

return { author, date, tags, link }
}

function parseLinkNode(linkNode: LinkTextNode) {
const linkMark = linkNode.marks.find(mark => mark.type === 'link')!
return {
title: linkNode.text,
link: linkMark.attrs.href,
}
}

function block2md(block: BlockNode): string {
switch (block.type) {
case 'heading': return heading2md(block)
case 'blockquote': return blockquote2md(block)
case 'paragraph': return paragraph2md(block)
// wave does not support table
// case 'table': return table2md(block)
case 'list': return list2md(block)
default: throw new Error(`Unknown block type: ${JSON.stringify(block)}`)
}
}

function inline2md(inline: InlineNode) {
switch (inline.type) {
case 'text': return text2md(inline)
case 'colorfulTag': return tag2md(inline)
case 'at': return at2md(inline)
default: throw new Error(`Unknown inline node type: ${JSON.stringify(inline)}`)
}
}

function heading2md(heading: HeadingNode) {
return heading.content.map(text2md).join('')
}

function heading2text(heading: HeadingNode) {
return heading.content.map(node => node.text).join('')
}

function paragraph2md(paragraph: ParagraphNode) {
return paragraph.content.map(inline2md).join('')
}

function blockquote2md(blockquote: BlockquoteNode) {
return blockquote.content.map(block => `> ${block2md(block)}`).join('\n')
}

function list2md(list: ListNode) {
return list.content
.map(block => prependBullet(block2md(block)))
.join('\n')
}

function tag2md(tag: TagNode) {
return mdInfo(tag.attrs['txt-content'])
}

function text2md(text: TextNode) {
return (text.marks || []).reduce((acc, mark) => {
switch (mark.type) {
case 'link': return `[${acc}](${mark.attrs.href})`
case 'bold': return `**${acc}**`
// Wave does not support inline code,
// use colored text to represent it.
case 'code': return mdInfo(acc)
default: return acc
}
}, text.text)
}

function at2md(at: AtNode) {
return mdAt(at.attrs.id)
}

function prependBullet(str: string) {
// Wave does not support list,
// use bullet emoji to represent it.
return `${mdComment('')} ${str}`
}

```
15 changes: 0 additions & 15 deletions playground/components.d.ts

This file was deleted.

Loading

0 comments on commit 8cade78

Please sign in to comment.