Skip to content

Commit

Permalink
增加支持标签
Browse files Browse the repository at this point in the history
  • Loading branch information
kscript committed Nov 28, 2023
1 parent 462a5d4 commit 1e5e0fe
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 26 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "markdown-downloader",
"version": "1.0.5",
"version": "1.0.6",
"description": "markdown文章下载",
"main": "dist/index.js",
"scripts": {
Expand Down
10 changes: 5 additions & 5 deletions src/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
Expand All @@ -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) => {
Expand All @@ -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)
}
Expand Down
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
28 changes: 19 additions & 9 deletions src/markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
---
Expand All @@ -36,8 +37,8 @@ const getMarkdown = (markdownBody) => {
// }[s1] || s))
}

export const tex2svg = (markdwonDoc) => {
return markdwonDoc.replace(/<ztext>(.*?)<\/ztext>/g, (s, s1) => {
export const tex2svg = (markdownDoc) => {
return markdownDoc.replace(/<ztext>(.*?)<\/ztext>/g, (s, s1) => {
const tex = decodeURIComponent(s1)
const svg = MathJax.tex2svg(tex)
svg.setAttribute('data-tex', tex)
Expand Down Expand Up @@ -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)
}
Expand All @@ -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 => {
Expand Down Expand Up @@ -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,
Expand All @@ -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()

Expand Down
3 changes: 2 additions & 1 deletion src/websites/csdn.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/websites/jianshu.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export const options = {
userName: '._3U4Smb ._1OhGeD',
userLink: '._3U4Smb ._1OhGeD',
invalid: '',
unpack: ''
unpack: '',
tag: '._18vaTa ._1OhGeD span'
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/websites/juejin.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ export const options = {
userName: '.username .name',
userLink: '.username',
invalid: 'style',
unpack: ''
unpack: '',
tag: '.article-end .tag-list .tag-item'
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/websites/oschina.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/websites/segmentfault.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/websites/zhihu.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
}

Expand All @@ -26,8 +27,8 @@ export const hook = {
item.parentElement.replaceChild(ztext, item)
})
},
formatContent (context, { markdwonDoc }) {
return tex2svg(markdwonDoc)
formatContent (context, { markdownDoc }) {
return tex2svg(markdownDoc)
}
}

Expand Down

0 comments on commit 1e5e0fe

Please sign in to comment.