diff --git a/package.json b/package.json index 55ab747..c3ce066 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "markdown-downloader", - "version": "1.0.7", + "version": "1.0.8", "description": "markdown文章下载", "main": "dist/index.js", "scripts": { diff --git a/src/markdown.js b/src/markdown.js index 2c03e2e..ccd161c 100644 --- a/src/markdown.js +++ b/src/markdown.js @@ -2,7 +2,7 @@ import merge from 'webpack-merge' import md5 from 'md5' import html2markdown from 'html-to-md' import 'mathjax/es5/tex-svg' -import { query, getExt, getText, getUrl, queryAll, insertAfter, getAttribute, formatDate, exec } from './utils' +import { query, getExt, getText, getUrl, queryAll, insertAfter, getAttribute, formatDate, exec, formatName } from './utils' const replace = (str, fn) => { fn = typeof fn === 'function' ? fn : (s) => s return str.replace(/\$\{(.*?)\}/g, (s, s1) => fn(s1.replace(/(^\s+|\s+$)/g, ''))) @@ -125,7 +125,7 @@ const extract = async (markdownBody, selectors, options, exec) => { const downloadName = item.getAttribute('downloadName') const downloadUrl = item.getAttribute('downloadUrl') if (downloadName && downloadUrl) { - item.src = './' + downloadName + item.src = './' + formatName(downloadName) return { name: downloadName, downloadUrl @@ -135,7 +135,7 @@ const extract = async (markdownBody, selectors, options, exec) => { const url = src.replace(/\?$/, '') const ext = getExt(url) const name = realName + '/' + md5(url) + (ext ? '.' + ext : '') - item.src = './' + name + item.src = './' + formatName(name) return { name, downloadUrl: url diff --git a/src/utils.js b/src/utils.js index 3100516..834f53f 100644 --- a/src/utils.js +++ b/src/utils.js @@ -2,8 +2,11 @@ import path from 'path-browserify' export const isBrowser = typeof window !== 'undefined' && window instanceof Object export const isExtension = isBrowser && window.chrome instanceof Object && window.chrome.runtime +export const formatName = (name) => { + return (typeof name === 'string' ? name : '').replace(/\s/g, '%20').split('?').shift() +} export const getExt = (fileName) => { - return path.parse(fileName).ext.slice(1) + return formatName(path.parse(fileName).ext.slice(1)) } export const query = (selector, context = document) => { if (selector instanceof NodeList || selector instanceof Node) { @@ -121,6 +124,7 @@ export const getLocalOptions = () => { export default { isBrowser, isExtension, + formatName, getExt, query, getText,