Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
95 lines (85 loc) · 2.82 KB

File metadata and controls

95 lines (85 loc) · 2.82 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
var path = require('path'),
moment = require('moment'),
hfm = require('hexo-front-matter'),
fs = require('hexo-fs'),
extend = require('extend');
// yfm = util.yfm,
// escape = util.escape;
/**
* Updates a post.
*
* @method update
* @param {str} model the type of model being updated
* @param {Object} post a post model
* @param {Object} update attributes to update
* @param {Function} callback
*/
module.exports = function (model, id, update, callback, hexo) {
function removeExtname(str) {
return str.substring(0, str.length - path.extname(str).length);
}
var post = hexo.model(model).get(id)
if (!post) {
return callback('Post not found');
}
var config = hexo.config,
slug = post.slug = hfm.escape(post.slug || post.title, config.filename_case),
layout = post.layout = (post.layout || config.default_layout).toLowerCase(),
date = post.date = post.date ? moment(post.date) : moment();
var split = hfm.split(post.raw),
frontMatter = split.data
compiled = hfm.parse([frontMatter, '---', split.content].join('\n'));
var preservedKeys = ['title', 'date', 'tags', 'categories', '_content', 'author'];
Object.keys(hexo.config.metadata || {}).forEach(function (key) {
preservedKeys.push(key);
});
var prev_full = post.full_source,
full_source = prev_full;
if (update.source && update.source !== post.source) {
// post.full_source only readable ~ see: /hexo/lib/models/post.js
full_source = hexo.source_dir + update.source
}
preservedKeys.forEach(function (attr) {
if (attr in update) {
compiled[attr] = update[attr]
}
});
compiled.date = moment(compiled.date).toDate()
delete update._content
var raw = hfm.stringify(compiled);
update.raw = raw
update.updated = moment()
// tags and cats are only getters now. ~ see: /hexo/lib/models/post.js
if ( typeof update.tags !== 'undefined' ) {
post.setTags(update.tags)
delete update.tags
}
if ( typeof update.categories !== 'undefined' ) {
post.setCategories(update.categories)
delete update.categories
}
extend(post, update)
post.save(function () {
// console.log(post.full_source, post.source)
fs.writeFile(full_source, raw, function(err){
if (err) return callback(err);
if (full_source !== prev_full) {
fs.unlinkSync(prev_full)
// move asset dir
var assetPrev = removeExtname(prev_full);
var assetDest = removeExtname(full_source);
fs.exists(assetPrev).then(function(exist) {
if (exist) {
fs.copyDir(assetPrev, assetDest).then(function() {
fs.rmdir(assetPrev);
});
}
});
}
hexo.source.process([post.source]).then(function () {
// console.log(post.full_source, post.source)
callback(null, hexo.model(model).get(id));
});
});
});
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.