Skip to content

Commit 1e5e0fe

Browse files
committed
增加支持标签
1 parent 462a5d4 commit 1e5e0fe

File tree

10 files changed

+42
-26
lines changed

10 files changed

+42
-26
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "markdown-downloader",
3-
"version": "1.0.5",
3+
"version": "1.0.6",
44
"description": "markdown文章下载",
55
"main": "dist/index.js",
66
"scripts": {

src/download.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ export const partTask = (items, handler, limit) => {
8787
const currentIndex = index++
8888
queue = queue.then(() => {
8989
return new Promise((resolve) => {
90-
Promise.all(handler(current, currentIndex)).then(datas => {
91-
result.push.apply(result, datas)
90+
Promise.all(handler(current, currentIndex)).then(data => {
91+
result.push.apply(result, data)
9292
})
9393
.finally(() => resolve(result))
9494
})
@@ -103,7 +103,7 @@ export const partRequest = (fileName, files, { requestLimit } = options) => {
103103
zip.file(file.name, blob)
104104
return blob
105105
}))
106-
return partTask(files, handler, requestLimit).then((datas) => {
106+
return partTask(files, handler, requestLimit).then(() => {
107107
return zip.generateAsync({
108108
type: "blob"
109109
}).then((content) => {
@@ -120,8 +120,8 @@ export const partRequest = (fileName, files, { requestLimit } = options) => {
120120
export const partDownload = (fileName, files, { partLimit } = options) => {
121121
const count = ~~(files.length / partLimit)
122122
return partTask(files, (files, index) => {
123-
const partMame = count >= 1 ? '-p' + (index + 1) + '-' + count : ''
124-
const name = fileName + partMame + '.zip'
123+
const partName = count >= 1 ? '-p' + (index + 1) + '-' + count : ''
124+
const name = fileName + partName + '.zip'
125125
return [partRequest(name, files, options)]
126126
}, partLimit)
127127
}

src/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import {
66
import { downloadMarkdown } from './markdown'
77

88
const extract = async (options, customOptions, hook) => {
9-
const datas = await downloadMarkdown(options, customOptions, hook)
10-
datas && sendMessage(datas)
11-
return datas
9+
const data = await downloadMarkdown(options, customOptions, hook)
10+
data && sendMessage(data)
11+
return data
1212
}
1313

1414
if (isExtension) {

src/markdown.js

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,20 @@ import { query, getExt, getText, getUrl, queryAll, insertAfter, getAttribute, fo
77
const setInfo = (data) => {
88
data = Object.assign({
99
date: formatDate('yyyy-MM-dd HH:mm:ss'),
10-
coypright: false,
10+
copyright: false,
1111
url: location.href,
1212
description: '转载',
13+
tag: []
1314
}, data instanceof Object ? data : {})
1415
return `---
1516
title: {{title}}
1617
date: {{date}}
17-
copyright: {{coypright}}
18+
copyright: {{copyright}}
1819
author: {{author}}
1920
home: {{home}}
2021
origin: {{origin}}
2122
url: {{url}}
22-
tag: {{tag}}
23+
tag: ${ data.tag && data.tag.length ? '\n - ' + data.tag.join('\n - ') : '' }
2324
categories: {{categories}}
2425
description: {{description}}
2526
---
@@ -36,8 +37,8 @@ const getMarkdown = (markdownBody) => {
3637
// }[s1] || s))
3738
}
3839

39-
export const tex2svg = (markdwonDoc) => {
40-
return markdwonDoc.replace(/<ztext>(.*?)<\/ztext>/g, (s, s1) => {
40+
export const tex2svg = (markdownDoc) => {
41+
return markdownDoc.replace(/<ztext>(.*?)<\/ztext>/g, (s, s1) => {
4142
const tex = decodeURIComponent(s1)
4243
const svg = MathJax.tex2svg(tex)
4344
svg.setAttribute('data-tex', tex)
@@ -101,6 +102,13 @@ export const formatMarkdownBody = (container, selectors, options, exec) => {
101102
item.parentElement.replaceChild(span, item)
102103
})
103104
}
105+
if (selectors.tag) {
106+
const tag = []
107+
queryAll(selectors.tag).map(item => {
108+
tag.push(item.innerText.replace(/(^[\n\s]+|[\n\s]+$)/g, ''))
109+
})
110+
options.context.tag = tag
111+
}
104112
if (options.link) {
105113
queryAll('a', markdownBody).map(item => item.href = item.title)
106114
}
@@ -116,7 +124,7 @@ export const formatMarkdownBody = (container, selectors, options, exec) => {
116124
}
117125

118126
const extract = async (markdownBody, selectors, options, exec) => {
119-
const { origin } = options
127+
const { origin, context } = options
120128
const fileName = getText(selectors.title) || document.title
121129
const realName = fileName.replace(/[\\\/\?<>:'\*\|]/g, '_')
122130
const files = queryAll('img', markdownBody).map(item => {
@@ -144,14 +152,15 @@ const extract = async (markdownBody, selectors, options, exec) => {
144152
origin: origin,
145153
author: getText(selectors.userName),
146154
home: getUrl(location.origin, getAttribute('href', selectors.userLink)),
155+
tag: context.tag,
147156
description: markdownBody.innerText.replace(/^([\n\s]+)/g, '').replace(/\n/g, ' ').slice(0, 50) + '...',
148157
})
149-
const markdwonDoc = html2markdown(info + getMarkdown(markdownBody), {})
158+
const markdownDoc = html2markdown(info + getMarkdown(markdownBody), {})
150159
const copyright = formatCopyRight(fileName)
151-
const content = await exec('formatContent', { markdownBody, markdwonDoc })
160+
const content = await exec('formatContent', { markdownBody, markdownDoc })
152161
files.push({
153162
name: realName + '.md',
154-
content: (content && typeof content === 'string' ? content : markdwonDoc) + '\n\n' + copyright
163+
content: (content && typeof content === 'string' ? content : markdownDoc) + '\n\n' + copyright
155164
})
156165
return {
157166
fileName,
@@ -168,6 +177,7 @@ export const downloadMarkdown = async (...rest) => {
168177
const verify = async (hookName, data) => {
169178
return await exec(hook[hookName], context, Object.assign(state, data instanceof Object ? data : {})) instanceof Object
170179
}
180+
options.context = context
171181

172182
if (await verify('beforeExtract')) return exec()
173183

src/websites/csdn.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ export const options = {
1212
userName: '.user-info .name',
1313
userLink: '.user-info .profile-intro-name-boxTop a',
1414
invalid: '',
15-
unpack: ''
15+
unpack: '',
16+
tag: '.blog-tags-box .tag-link'
1617
}
1718
}
1819

src/websites/jianshu.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ export const options = {
1313
userName: '._3U4Smb ._1OhGeD',
1414
userLink: '._3U4Smb ._1OhGeD',
1515
invalid: '',
16-
unpack: ''
16+
unpack: '',
17+
tag: '._18vaTa ._1OhGeD span'
1718
}
1819
}
1920

src/websites/juejin.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ export const options = {
1616
userName: '.username .name',
1717
userLink: '.username',
1818
invalid: 'style',
19-
unpack: ''
19+
unpack: '',
20+
tag: '.article-end .tag-list .tag-item'
2021
}
2122
}
2223

src/websites/oschina.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ export const options = {
1212
userName: '.article-box .article-box__meta .item-list .item:nth-child(2) a',
1313
userLink: '.article-box .article-box__meta .item-list .item:nth-child(2) a',
1414
invalid: '',
15-
unpack: ''
15+
unpack: '',
16+
tag: '.article-box__group .group-card__name'
1617
}
1718
}
1819

src/websites/segmentfault.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ export const options = {
1212
userName: '.card-body picture+strong',
1313
userLink: '.card-body h1+div a:nth-child(1)',
1414
invalid: '',
15-
unpack: ''
15+
unpack: '',
16+
tag: '.article-wrap .badge-tag'
1617
}
1718
}
1819

src/websites/zhihu.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ export const options = {
1414
userName: '.AuthorInfo-name .UserLink-link',
1515
userLink: '.AuthorInfo-name .UserLink-link',
1616
invalid: 'noscript,.ZVideoLinkCard-author',
17-
unpack: 'p,figure'
17+
unpack: 'p,figure',
18+
tag: '.TopicList .Tag-content'
1819
}
1920
}
2021

@@ -26,8 +27,8 @@ export const hook = {
2627
item.parentElement.replaceChild(ztext, item)
2728
})
2829
},
29-
formatContent (context, { markdwonDoc }) {
30-
return tex2svg(markdwonDoc)
30+
formatContent (context, { markdownDoc }) {
31+
return tex2svg(markdownDoc)
3132
}
3233
}
3334

0 commit comments

Comments
 (0)