Skip to content

Commit 5a5871f

Browse files
committed
修复图片url
1 parent cbe2ba3 commit 5a5871f

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed

package.json

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

src/markdown.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import merge from 'webpack-merge'
22
import md5 from 'md5'
33
import html2markdown from 'html-to-md'
44
import 'mathjax/es5/tex-svg'
5-
import { query, getExt, getText, getUrl, queryAll, insertAfter, getAttribute, formatDate, exec } from './utils'
5+
import { query, getExt, getText, getUrl, queryAll, insertAfter, getAttribute, formatDate, exec, formatName } from './utils'
66
const replace = (str, fn) => {
77
fn = typeof fn === 'function' ? fn : (s) => s
88
return str.replace(/\$\{(.*?)\}/g, (s, s1) => fn(s1.replace(/(^\s+|\s+$)/g, '')))
@@ -125,7 +125,7 @@ const extract = async (markdownBody, selectors, options, exec) => {
125125
const downloadName = item.getAttribute('downloadName')
126126
const downloadUrl = item.getAttribute('downloadUrl')
127127
if (downloadName && downloadUrl) {
128-
item.src = './' + downloadName
128+
item.src = './' + formatName(downloadName)
129129
return {
130130
name: downloadName,
131131
downloadUrl
@@ -135,7 +135,7 @@ const extract = async (markdownBody, selectors, options, exec) => {
135135
const url = src.replace(/\?$/, '')
136136
const ext = getExt(url)
137137
const name = realName + '/' + md5(url) + (ext ? '.' + ext : '')
138-
item.src = './' + name
138+
item.src = './' + formatName(name)
139139
return {
140140
name,
141141
downloadUrl: url

src/utils.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ import path from 'path-browserify'
22

33
export const isBrowser = typeof window !== 'undefined' && window instanceof Object
44
export const isExtension = isBrowser && window.chrome instanceof Object && window.chrome.runtime
5+
export const formatName = (name) => {
6+
return (typeof name === 'string' ? name : '').replace(/\s/g, '%20').split('?').shift()
7+
}
58
export const getExt = (fileName) => {
6-
return path.parse(fileName).ext.slice(1)
9+
return formatName(path.parse(fileName).ext.slice(1))
710
}
811
export const query = (selector, context = document) => {
912
if (selector instanceof NodeList || selector instanceof Node) {
@@ -121,6 +124,7 @@ export const getLocalOptions = () => {
121124
export default {
122125
isBrowser,
123126
isExtension,
127+
formatName,
124128
getExt,
125129
query,
126130
getText,

0 commit comments

Comments
 (0)