From 1e5e0fe04dce1edf1595af87ea37164a63184ef6 Mon Sep 17 00:00:00 2001 From: kscript <1258@live.cn> Date: Tue, 28 Nov 2023 11:57:31 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=94=AF=E6=8C=81=E6=A0=87?= =?UTF-8?q?=E7=AD=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- src/download.js | 10 +++++----- src/index.js | 6 +++--- src/markdown.js | 28 +++++++++++++++++++--------- src/websites/csdn.js | 3 ++- src/websites/jianshu.js | 3 ++- src/websites/juejin.js | 3 ++- src/websites/oschina.js | 3 ++- src/websites/segmentfault.js | 3 ++- src/websites/zhihu.js | 7 ++++--- 10 files changed, 42 insertions(+), 26 deletions(-) diff --git a/package.json b/package.json index d15203b..ede2268 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "markdown-downloader", - "version": "1.0.5", + "version": "1.0.6", "description": "markdown文章下载", "main": "dist/index.js", "scripts": { diff --git a/src/download.js b/src/download.js index 8e3772f..49b682f 100644 --- a/src/download.js +++ b/src/download.js @@ -87,8 +87,8 @@ export const partTask = (items, handler, limit) => { const currentIndex = index++ queue = queue.then(() => { return new Promise((resolve) => { - Promise.all(handler(current, currentIndex)).then(datas => { - result.push.apply(result, datas) + Promise.all(handler(current, currentIndex)).then(data => { + result.push.apply(result, data) }) .finally(() => resolve(result)) }) @@ -103,7 +103,7 @@ export const partRequest = (fileName, files, { requestLimit } = options) => { zip.file(file.name, blob) return blob })) - return partTask(files, handler, requestLimit).then((datas) => { + return partTask(files, handler, requestLimit).then(() => { return zip.generateAsync({ type: "blob" }).then((content) => { @@ -120,8 +120,8 @@ export const partRequest = (fileName, files, { requestLimit } = options) => { export const partDownload = (fileName, files, { partLimit } = options) => { const count = ~~(files.length / partLimit) return partTask(files, (files, index) => { - const partMame = count >= 1 ? '-p' + (index + 1) + '-' + count : '' - const name = fileName + partMame + '.zip' + const partName = count >= 1 ? '-p' + (index + 1) + '-' + count : '' + const name = fileName + partName + '.zip' return [partRequest(name, files, options)] }, partLimit) } diff --git a/src/index.js b/src/index.js index 17a10cb..299477c 100644 --- a/src/index.js +++ b/src/index.js @@ -6,9 +6,9 @@ import { import { downloadMarkdown } from './markdown' const extract = async (options, customOptions, hook) => { - const datas = await downloadMarkdown(options, customOptions, hook) - datas && sendMessage(datas) - return datas + const data = await downloadMarkdown(options, customOptions, hook) + data && sendMessage(data) + return data } if (isExtension) { diff --git a/src/markdown.js b/src/markdown.js index d89b16c..04093b6 100644 --- a/src/markdown.js +++ b/src/markdown.js @@ -7,19 +7,20 @@ import { query, getExt, getText, getUrl, queryAll, insertAfter, getAttribute, fo const setInfo = (data) => { data = Object.assign({ date: formatDate('yyyy-MM-dd HH:mm:ss'), - coypright: false, + copyright: false, url: location.href, description: '转载', + tag: [] }, data instanceof Object ? data : {}) return `--- title: {{title}} date: {{date}} - copyright: {{coypright}} + copyright: {{copyright}} author: {{author}} home: {{home}} origin: {{origin}} url: {{url}} - tag: {{tag}} + tag: ${ data.tag && data.tag.length ? '\n - ' + data.tag.join('\n - ') : '' } categories: {{categories}} description: {{description}} --- @@ -36,8 +37,8 @@ const getMarkdown = (markdownBody) => { // }[s1] || s)) } -export const tex2svg = (markdwonDoc) => { - return markdwonDoc.replace(/(.*?)<\/ztext>/g, (s, s1) => { +export const tex2svg = (markdownDoc) => { + return markdownDoc.replace(/(.*?)<\/ztext>/g, (s, s1) => { const tex = decodeURIComponent(s1) const svg = MathJax.tex2svg(tex) svg.setAttribute('data-tex', tex) @@ -101,6 +102,13 @@ export const formatMarkdownBody = (container, selectors, options, exec) => { item.parentElement.replaceChild(span, item) }) } + if (selectors.tag) { + const tag = [] + queryAll(selectors.tag).map(item => { + tag.push(item.innerText.replace(/(^[\n\s]+|[\n\s]+$)/g, '')) + }) + options.context.tag = tag + } if (options.link) { queryAll('a', markdownBody).map(item => item.href = item.title) } @@ -116,7 +124,7 @@ export const formatMarkdownBody = (container, selectors, options, exec) => { } const extract = async (markdownBody, selectors, options, exec) => { - const { origin } = options + const { origin, context } = options const fileName = getText(selectors.title) || document.title const realName = fileName.replace(/[\\\/\?<>:'\*\|]/g, '_') const files = queryAll('img', markdownBody).map(item => { @@ -144,14 +152,15 @@ const extract = async (markdownBody, selectors, options, exec) => { origin: origin, author: getText(selectors.userName), home: getUrl(location.origin, getAttribute('href', selectors.userLink)), + tag: context.tag, description: markdownBody.innerText.replace(/^([\n\s]+)/g, '').replace(/\n/g, ' ').slice(0, 50) + '...', }) - const markdwonDoc = html2markdown(info + getMarkdown(markdownBody), {}) + const markdownDoc = html2markdown(info + getMarkdown(markdownBody), {}) const copyright = formatCopyRight(fileName) - const content = await exec('formatContent', { markdownBody, markdwonDoc }) + const content = await exec('formatContent', { markdownBody, markdownDoc }) files.push({ name: realName + '.md', - content: (content && typeof content === 'string' ? content : markdwonDoc) + '\n\n' + copyright + content: (content && typeof content === 'string' ? content : markdownDoc) + '\n\n' + copyright }) return { fileName, @@ -168,6 +177,7 @@ export const downloadMarkdown = async (...rest) => { const verify = async (hookName, data) => { return await exec(hook[hookName], context, Object.assign(state, data instanceof Object ? data : {})) instanceof Object } + options.context = context if (await verify('beforeExtract')) return exec() diff --git a/src/websites/csdn.js b/src/websites/csdn.js index 7f49819..b244b18 100644 --- a/src/websites/csdn.js +++ b/src/websites/csdn.js @@ -12,7 +12,8 @@ export const options = { userName: '.user-info .name', userLink: '.user-info .profile-intro-name-boxTop a', invalid: '', - unpack: '' + unpack: '', + tag: '.blog-tags-box .tag-link' } } diff --git a/src/websites/jianshu.js b/src/websites/jianshu.js index db66e33..4793c67 100644 --- a/src/websites/jianshu.js +++ b/src/websites/jianshu.js @@ -13,7 +13,8 @@ export const options = { userName: '._3U4Smb ._1OhGeD', userLink: '._3U4Smb ._1OhGeD', invalid: '', - unpack: '' + unpack: '', + tag: '._18vaTa ._1OhGeD span' } } diff --git a/src/websites/juejin.js b/src/websites/juejin.js index 7a70f3e..517d98a 100644 --- a/src/websites/juejin.js +++ b/src/websites/juejin.js @@ -16,7 +16,8 @@ export const options = { userName: '.username .name', userLink: '.username', invalid: 'style', - unpack: '' + unpack: '', + tag: '.article-end .tag-list .tag-item' } } diff --git a/src/websites/oschina.js b/src/websites/oschina.js index c12cb14..f1d9252 100644 --- a/src/websites/oschina.js +++ b/src/websites/oschina.js @@ -12,7 +12,8 @@ export const options = { userName: '.article-box .article-box__meta .item-list .item:nth-child(2) a', userLink: '.article-box .article-box__meta .item-list .item:nth-child(2) a', invalid: '', - unpack: '' + unpack: '', + tag: '.article-box__group .group-card__name' } } diff --git a/src/websites/segmentfault.js b/src/websites/segmentfault.js index 5f0fc1f..f7d7c1f 100644 --- a/src/websites/segmentfault.js +++ b/src/websites/segmentfault.js @@ -12,7 +12,8 @@ export const options = { userName: '.card-body picture+strong', userLink: '.card-body h1+div a:nth-child(1)', invalid: '', - unpack: '' + unpack: '', + tag: '.article-wrap .badge-tag' } } diff --git a/src/websites/zhihu.js b/src/websites/zhihu.js index d13a00b..3f59e2f 100644 --- a/src/websites/zhihu.js +++ b/src/websites/zhihu.js @@ -14,7 +14,8 @@ export const options = { userName: '.AuthorInfo-name .UserLink-link', userLink: '.AuthorInfo-name .UserLink-link', invalid: 'noscript,.ZVideoLinkCard-author', - unpack: 'p,figure' + unpack: 'p,figure', + tag: '.TopicList .Tag-content' } } @@ -26,8 +27,8 @@ export const hook = { item.parentElement.replaceChild(ztext, item) }) }, - formatContent (context, { markdwonDoc }) { - return tex2svg(markdwonDoc) + formatContent (context, { markdownDoc }) { + return tex2svg(markdownDoc) } }