Skip to content

Commit

Permalink
修复图片url
Browse files Browse the repository at this point in the history
  • Loading branch information
kscript committed Jul 31, 2024
1 parent cbe2ba3 commit 5a5871f
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 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.7",
"version": "1.0.8",
"description": "markdown文章下载",
"main": "dist/index.js",
"scripts": {
Expand Down
6 changes: 3 additions & 3 deletions src/markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, '')))
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 5 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -121,6 +124,7 @@ export const getLocalOptions = () => {
export default {
isBrowser,
isExtension,
formatName,
getExt,
query,
getText,
Expand Down

0 comments on commit 5a5871f

Please sign in to comment.