feat: Batch convert local images (#144)

This commit is contained in:
xw 2022-03-23 15:07:41 +08:00 committed by GitHub
parent dba40a7254
commit c0d56ff27b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 102 additions and 2 deletions

View File

@ -41,6 +41,7 @@ Markdown 文档自动即时渲染为微信图文,让你不再为微信文章
- [x] 支持多图上传,可自定义配置图床
- [x] 支持自定义上传逻辑
- [x] 支持在编辑框右键弹出功能选项卡
- [x] 支持批量转换本地图片为线上图片
## 目前支持哪些图床

View File

@ -21,6 +21,7 @@
<el-col
:span="12"
class="codeMirror-wrapper"
ref="codeMirrorWrapper"
@contextmenu.prevent.native="openMenu($event)"
>
<textarea
@ -158,9 +159,107 @@ export default {
this.initEditor()
this.initCssEditor()
this.onEditorRefresh()
this.mdLocalToRemote()
})
},
methods: {
// markdown 线
// todo
mdLocalToRemote() {
const vm = this
const dom = this.$refs.codeMirrorWrapper.$el
dom.ondragover = (evt) => evt.preventDefault()
dom.ondrop = async (evt) => {
evt.preventDefault()
for (const item of evt.dataTransfer.items) {
item.getAsFileSystemHandle().then(async (handle) => {
if (handle.kind === `directory`) {
const list = await showFileStructure(handle)
const md = await getMd({ list })
uploadMdImg({ md, list })
} else {
const file = await handle.getFile()
console.log(`file`, file)
}
})
}
}
// md
async function getMd({ list }) {
return new Promise((resolve, reject) => {
const { path, file } = list.find((item) => item.path.match(/\.md$/))
const reader = new FileReader()
reader.readAsText(file, `UTF-8`)
reader.onload = (evt) => {
resolve({
str: evt.target.result,
file,
path,
})
}
})
}
// md
async function uploadMdImg({ md, list }) {
const mdImgList = [
...(md.str.matchAll(/!\[(.*?)\]\((.*?)\)/gm) || []),
].filter((item) => {
return item //
})
const root = md.path.match(/.+?\//)[0]
const resList = await Promise.all(
mdImgList.map((item) => {
return new Promise((resolve, reject) => {
let [, , matchStr] = item
matchStr = matchStr.replace(/^.\//, ``) // 处理 ./img/ img/ 统一相对路径风格
const { file } =
list.find((f) => f.path === `${root}${matchStr}`) || {}
vm.uploadImage(file, (url) => {
resolve({ matchStr, url })
})
})
})
)
resList.forEach((item) => {
md.str = md.str
.replace(`](./${item.matchStr})`, `](${item.url})`)
.replace(`](${item.matchStr})`, `](${item.url})`)
})
vm.editor.setValue(md.str)
console.log(`resList`, resList, md.str)
}
//
async function showFileStructure(root) {
const result = []
let cwd = ``
try {
const dirs = [root]
for (const dir of dirs) {
cwd += dir.name + `/`
for await (const [, handle] of dir) {
if (handle.kind === `file`) {
result.push({
path: cwd + handle.name,
file: await handle.getFile(),
})
} else {
result.push({
path: cwd + handle.name + `/`,
})
dirs.push(handle)
}
}
}
} catch (err) {
console.error(err)
}
return result
}
},
initEditor() {
this.initEditorEntity()
this.editor.on(`change`, (cm, e) => {
@ -260,7 +359,7 @@ export default {
}
return true
},
uploadImage(file) {
uploadImage(file, cb) {
this.isImgLoading = true
toBase64(file)
.then((base64Content) => {
@ -268,7 +367,7 @@ export default {
.fileUpload(base64Content, file)
.then((url) => {
console.log(url)
this.uploaded(url)
cb ? cb(url) : this.uploaded(url)
})
.catch((err) => {
this.$message.error(err.message)