提取TAG
This commit is contained in:
parent
1191df9d54
commit
7fb1a52102
@ -4,15 +4,10 @@ import { lexer, marked } from 'marked';
|
||||
import hljs from 'highlight.js';
|
||||
import he from 'he';
|
||||
|
||||
// 读取所有 blog 中的 md 文件, 以此来获取所有的 tag
|
||||
const findTags = (item) => {
|
||||
const list = []
|
||||
if (item.type === 'em') list.push(item.text)
|
||||
if (item.type === 'strong') list.push(item.text)
|
||||
item.tokens?.forEach(token => {
|
||||
findTags(token).forEach(tag => list.push(tag))
|
||||
});
|
||||
return list
|
||||
// 正则取以#号开头的标签
|
||||
const findTags = (item, list = []) => {
|
||||
if (item.type === 'text') list.push(...(item.text?.match(/(?<=#)\S+/g) || []))
|
||||
return [...list, ...(item.tokens?.map(token => findTags(token)) || [])].flat()
|
||||
}
|
||||
|
||||
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) => {
|
||||
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.tags = lexer(body.content).map(token => findTags(token)).flat()
|
||||
await blog.setItem(body.id, 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
|
||||
}
|
||||
|
||||
// 处理其他请求
|
||||
|
@ -1,25 +1,12 @@
|
||||
import fs from 'fs';
|
||||
import { lexer } from 'marked';
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
const tags = useStorage('tags')
|
||||
|
||||
// 处理 GET 请求
|
||||
if (event.node.req.method === 'GET') {
|
||||
// 读取所有 blog 中的 md 文件, 以此来获取所有的 tag
|
||||
const loadtags = (item) => {
|
||||
const list = [];
|
||||
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();
|
||||
return await tags.getKeys().then(keys => {
|
||||
return Promise.all(keys.map(async key => {
|
||||
return tags.getItem(key).then(data => data)
|
||||
}))
|
||||
})
|
||||
}
|
||||
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user