-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
67 lines (54 loc) · 2.41 KB
/
index.js
File metadata and controls
67 lines (54 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
const ipfsUploader = require('./lib/ipfs-uploader');
const config = require('./lib/config');
const {streamToString, getRouteContent} = require('./lib/helpers');
const fs = require('fs');
const pluginConfig = config(hexo);
hexo.extend.filter.register('after_generate', async function () {
const {route} = this;
try {
await ipfsUploader.initializeIPFS(pluginConfig);
const posts = hexo.locals.get('posts');
for (const post of posts.data) {
if (post && post.ipfs === true) {
const postPath = post.path;
const htmlPath = postPath.replace(/\.md$/, '.html');
const htmlContent = await getRouteContent(route, htmlPath);
if (htmlContent) {
const cid = await ipfsUploader.uploadToIPFS(htmlContent);
if (!post.cids) {
post.cids = [];
}
post.cids.unshift(cid);
const sourceFilePath = post.full_source;
let postContent = fs.readFileSync(sourceFilePath, 'utf8');
const cidsRegex = /cids:(\s*- .*$)*/m;
const newCidsSection = `cids:\n - ${cid}${post.cids.slice(1).map(c => `\n - ${c}`).join('')}`;
if (cidsRegex.test(postContent)) {
postContent = postContent.replace(cidsRegex, newCidsSection);
} else {
const frontMatterEnd = postContent.indexOf('---', postContent.indexOf('---') + 1);
postContent = postContent.slice(0, frontMatterEnd) + `${newCidsSection}\n` + postContent.slice(frontMatterEnd);
}
fs.writeFileSync(sourceFilePath, postContent);
hexo.log.info(`Uploaded ${post.title} to IPFS with CID: ${cid}`);
} else {
hexo.log.warn(`Could not find HTML content for post: ${post.title}`);
}
}
}
} catch (error) {
hexo.log.error('Error uploading to IPFS:', error);
}
});
hexo.extend.helper.register('getLatestIpfsCID', function (page) {
if (page.cids && page.cids.length > 0) {
return page.cids[0];
}
return null
})
hexo.extend.helper.register('getIpfsLink', function (page) {
if (page.ipfs && page.ipfs.cid) {
return `https://ipfs.io/ipfs/${page.ipfs.cid}`;
}
return '';
});