提取TAG

This commit is contained in:
2024-08-24 09:44:22 +08:00
parent 1191df9d54
commit 7fb1a52102
2 changed files with 11 additions and 56 deletions

View File

@ -4,15 +4,10 @@ import { lexer, marked } from 'marked';
import hljs from 'highlight.js'; import hljs from 'highlight.js';
import he from 'he'; import he from 'he';
// 读取所有 blog 中的 md 文件, 以此来获取所有的 tag // 正则取以#号开头的标签
const findTags = (item) => { const findTags = (item, list = []) => {
const list = [] if (item.type === 'text') list.push(...(item.text?.match(/(?<=#)\S+/g) || []))
if (item.type === 'em') list.push(item.text) return [...list, ...(item.tokens?.map(token => findTags(token)) || [])].flat()
if (item.type === 'strong') list.push(item.text)
item.tokens?.forEach(token => {
findTags(token).forEach(tag => list.push(tag))
});
return list
} }
export default defineEventHandler(async event => { export default defineEventHandler(async event => {
@ -81,37 +76,10 @@ export default defineEventHandler(async event => {
body.html = marked(body.content, { breaks: true }).replace(regex, (match, p1, p2) => { body.html = marked(body.content, { breaks: true }).replace(regex, (match, p1, p2) => {
return `<code class="${p1} hljs">${hljs.highlightAuto(he.decode(p2)).value}</code>` return `<code class="${p1} hljs">${hljs.highlightAuto(he.decode(p2)).value}</code>`
}) })
// 提取标题
body.title = lexer(body.content).find(x => x.type === 'heading' && x.depth === 1)?.text || '' body.title = lexer(body.content).find(x => x.type === 'heading' && x.depth === 1)?.text || ''
body.tags = lexer(body.content).map(token => findTags(token)).flat()
await blog.setItem(body.id, body) await blog.setItem(body.id, body)
return body return body
//const { content } = await readBody(event)
//if (!content) {
// event.node.res.statusCode = 400
// return { success: false, message: '内容不可为空' }
//}
//const date = Date.now()
//const data = { id: v4(), content, createdAt:date, updatedAt:date, user_id }
//await blog.setItem(data.id, data)
//data.html = marked(data.content, { breaks: true })
//const regex = /<code\s+class="(.*)"\s*>([\s\S]*?)<\/code>/g;
//data.html = data.html.replace(regex, (match, p1, p2) => {
// const html = hljs.highlightAuto(he.decode(p2)).value
// return `<code class="${p1} hljs">${html}</code>`
//})
//// 提取标题
//const tokens = lexer(data.content)
//const title = tokens.find(item => item.type === 'heading' && item.depth === 1)
//data.title = title ? title.text : ''
//// 提取标签
//data.tags = []
//tokens.forEach(token => {
// findTags(token).forEach(tag => data.tags.push(tag))
//})
//// 将时间戳转换为 UTC 时间
//data.createdAt = new Date(data.createdAt)
//data.updatedAt = new Date(data.updatedAt)
//return data
} }
// 处理其他请求 // 处理其他请求

View File

@ -1,25 +1,12 @@
import fs from 'fs';
import { lexer } from 'marked';
export default defineEventHandler(async event => { export default defineEventHandler(async event => {
const tags = useStorage('tags')
// 处理 GET 请求
if (event.node.req.method === 'GET') { if (event.node.req.method === 'GET') {
// 读取所有 blog 中的 md 文件, 以此来获取所有的 tag return await tags.getKeys().then(keys => {
const loadtags = (item) => { return Promise.all(keys.map(async key => {
const list = []; return tags.getItem(key).then(data => data)
if (item.type === 'em') list.push(item.text); }))
if (item.type === 'strong') list.push(item.text); })
item.tokens?.forEach(token => {
loadtags(token).forEach(tag => list.push(tag));
});
return list;
}
const tags = fs.readdirSync('data/blog/markdown').map(file => {
const content = fs.readFileSync(`data/blog/markdown/${file}`, 'utf-8');
return loadtags({ tokens: lexer(content) })
});
return tags.flat();
} }
}) })