Skip to content

Commit

Permalink
feat: upload lakeboard cover image to github
Browse files Browse the repository at this point in the history
  • Loading branch information
fish.yu committed Apr 17, 2024
1 parent 5e7dff7 commit 9d4a4e6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
12 changes: 9 additions & 3 deletions lib/download.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const lodash = require('lodash');
const Queue = require('queue');
const filenamify = require('filenamify');
const crypto = require('crypto');
const YuqueClient = require('./yuque');
const GitHubClient = require('./github')
const { isPost, decoratePost, formatRaw, formatDate } = require('./utils');
const { isPost, decoratePost, formatRaw, formatDate, downloadImage } = require('./utils');

// 需要提取的文章属性字段
const PICK_PROPERTY = [
Expand Down Expand Up @@ -129,9 +130,14 @@ class Downloader {
// }

let mdBody = formatRaw(body);
// 脑图处理
// 处理脑图
if (format === 'lakeboard' && cover) {
mdBody = `![${title}](${cover})`
// 上传图片到github
const content = await downloadImage(cover);
const imageName = crypto.randomUUID() + cover.split('.').pop();
const imageUrl = `${repo_name}/_images/${imageName}`;
await gh.writeFile(imageUrl, content)
mdBody = `![${title}](${imageUrl})`
}
const content = decoratePost(title, mdBody, `https://www.yuque.com/${client.namespace}/${slug}`)
await gh.writeFile(`${repo_name}/${_fullName}`, content)
Expand Down
13 changes: 13 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const moment = require('moment');
const lodash = require('lodash');
const axios = require('axios');

const formatMarkdown = (() => {
let prettier;
try {
Expand Down Expand Up @@ -47,3 +49,14 @@ exports.isPost = (post) => {
exports.formatDate = (date) => {
return moment(new Date(date).toISOString()).format('YYYY-MM-DD');
}

exports.downloadImage = (src) => {
return new Promise((resolve, reject) => {
axios.get(src).then((res) => {
const base64 = Buffer.from(res.data, 'binary').toString('base64');
resolve(`data:image/png;base64,${base64}`)
}).catch((err) => {
reject(err);
})
})
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"license": "ISC",
"dependencies": {
"@yuque/sdk": "^1.1.1",
"axios": "^1.6.8",
"dotenv-flow": "^3.2.0",
"filenamify": "^4.1.0",
"github-api": "^3.4.0",
Expand Down

0 comments on commit 9d4a4e6

Please sign in to comment.