Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 | 14x 14x 14x 14x 14x 14x 14x 14x 14x 168x 168x 22x 22x 10x 10x 10x 10x 10x 10x 10x 10x 10x 1x 1x 10x 1x 3x 3x 4x 5x 5x 20x 5x 4x 4x 8x 8x 4x 4x 4x 4x 4x 4x 4x 8x 8x 4x 1x 1x 1x 1x 1x 8x 8x 8x 8x 8x 8x 2x 2x 3x 3x 3x 3x 3x 3x 3x 3x 3x 4x 2x 2x 2x 6x 2x 9x 9x 3x 3x 3x 3x 3x 3x 3x 3x 9x 3x 3x 3x 3x 3x 2x 2x 2x 2x 2x 2x 2x 2x 2x 3x 3x 3x 3x 2x 2x 8x 8x 9x 8x 8x 8x 4x 4x 4x 4x 8x 8x 28x 28x 28x 28x 28x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 9x 8x 8x 8x 8x 8x 8x 2x 8x 10x 10x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 6x 1x 5x 5x 12x 2x 2x 11x 14x 1x 1x 1x 1x 1x 14x | (function () {
"use strict";
var pendingSaveContext = null;
var savedTitle = "";
var pathAliases = Object.create(null);
function toPlainData(entry) {
var data = entry && entry.get && entry.get("data");
Iif (data && typeof data.toJS === "function") return data.toJS();
Iif (data && typeof data === "object") return data;
var fields = [
"title", "description", "body", "category", "publishedAt", "updatedAt",
"draft", "featured", "cover", "coverAlt", "series", "seriesOrder",
];
return fields.reduce(function (result, field) {
result[field] = entry && entry.getIn && entry.getIn(["data", field]);
return result;
}, {});
}
function titleFromPath(path) {
var match = String(path || "").match(/\/([^/]+)\.md$/);
return match ? match[1] : "";
}
function isNotFound(error) {
var status = error && (error.status || error.statusCode);
return status === 404 || /not found/i.test(String(error && error.message));
}
function consumeSaveContext() {
var context = pendingSaveContext;
pendingSaveContext = null;
Iif (!context) throw new Error("文章保存上下文丢失,请刷新后台后重试。");
return context;
}
async function assertUniqueTitle(implementation, context, targetPath) {
Iif (!context.newRecord && targetPath === context.currentPath) return;
try {
var existing = await implementation.getEntry(targetPath);
Eif (existing && existing.data) {
throw new Error("已存在同名文章,请使用不同标题。");
}
return;
} catch (error) {
if (isNotFound(error)) return;
throw error;
}
}
function fieldName(asset) {
Iif (!asset || !asset.field) return "";
return typeof asset.field.get === "function"
? asset.field.get("name")
: asset.field.name || "";
}
function publicPath(filePath) {
return "/" + String(filePath || "").replace(/^public\//, "");
}
function toBase64(file) {
return new Promise(function (resolve, reject) {
var reader = new FileReader();
reader.onload = function () {
resolve(String(reader.result || "").split("base64,")[1] || "");
};
reader.onerror = function () { reject(reader.error); };
reader.readAsDataURL(file);
});
}
function replaceAll(raw, replacements) {
var result = String(raw || "");
replacements.forEach(function (replacement) {
result = result.split(replacement.from).join(replacement.to);
});
return result;
}
async function loadAllPosts(implementation) {
var loader = typeof implementation.allEntriesByFolder === "function"
? implementation.allEntriesByFolder.bind(implementation)
: implementation.entriesByFolder.bind(implementation);
return loader("src/content/posts", "md", 100);
}
async function prepareRename(implementation, entry, context) {
var oldTitle = titleFromPath(context.currentPath);
if (context.newRecord || !oldTitle || oldTitle === context.title) return entry;
var oldArticlePath = DecapEditorialDomain.publicArticlePath(oldTitle);
var newArticlePath = DecapEditorialDomain.publicArticlePath(context.title);
var oldMediaPublic = "/images/posts/" + DecapMediaDomain.urlTitleSegment(oldTitle) + "/";
var newMediaPublic = "/images/posts/" + DecapMediaDomain.urlTitleSegment(context.title) + "/";
var replacements = [
{ from: oldArticlePath, to: newArticlePath },
{
from: "/posts/" + DecapMediaDomain.urlTitleSegment(oldTitle) + "/",
to: "/posts/" + DecapMediaDomain.urlTitleSegment(context.title) + "/",
},
{ from: oldMediaPublic, to: newMediaPublic },
{
from: "/images/posts/" + oldTitle + "/",
to: "/images/posts/" + context.title + "/",
},
];
var dataFiles = (entry.dataFiles || []).map(function (dataFile) {
return Object.assign({}, dataFile, {
raw: replaceAll(dataFile.raw, replacements),
});
});
var knownPaths = new Set(dataFiles.map(function (file) { return file.path; }));
var posts = await loadAllPosts(implementation);
(posts || []).forEach(function (post) {
var postPath = post.file && post.file.path;
var raw = typeof post.data === "string" ? post.data : "";
var rewritten = replaceAll(raw, replacements);
Eif (postPath && !knownPaths.has(postPath) && rewritten !== raw) {
dataFiles.push({
path: postPath,
slug: postPath.split("/").pop().replace(/\.md$/, ""),
raw: rewritten,
});
}
});
var oldFolder = DecapEditorialDomain.mediaFolder(oldTitle);
var newFolder = DecapEditorialDomain.mediaFolder(context.title);
var media = typeof implementation.getMedia === "function"
? await implementation.getMedia(oldFolder)
: [];
var assets = (entry.assets || []).slice();
context.proxyOldMediaPaths = [];
if (media && media.length && typeof implementation.getMediaFile === "function") {
var filesToLoad = implementation.__decapBackendName === "github"
? [media[0]]
: media;
for (var index = 0; index < filesToLoad.length; index += 1) {
var mediaFile = filesToLoad[index];
var loaded = await implementation.getMediaFile(mediaFile.path);
var file = loaded.file || loaded.fileObj;
Iif (!file) throw new Error("无法读取文章媒体:" + mediaFile.path);
var asset = {
path: implementation.__decapBackendName === "proxy"
? newFolder + "/" + mediaFile.name
: mediaFile.path,
fileObj: file,
url: loaded.url || loaded.displayURL,
toBase64: function (sourceFile) {
return function () { return toBase64(sourceFile); };
}(file),
};
Iif (implementation.__decapBackendName === "github") {
asset.newPath = newFolder + "/" + mediaFile.name;
} else {
context.proxyOldMediaPaths.push(mediaFile.path);
}
assets.push(asset);
}
}
return Object.assign({}, entry, { dataFiles: dataFiles, assets: assets });
}
function rewriteReference(raw, oldPath, newPath) {
var result = String(raw || "");
var variants = [
publicPath(oldPath),
publicPath(oldPath).replace(/ /g, "%20"),
oldPath,
];
variants.forEach(function (value) {
result = result.split(value).join(newPath);
});
return result;
}
async function prepareAssets(implementation, entry, context) {
var assets = entry.assets || [];
if (!assets.length) return entry;
Iif (!window.DecapMediaDomain || !window.DecapMediaProcessor) {
throw new Error("图片处理模块未加载,请刷新后台后重试。");
}
var folder = DecapEditorialDomain.mediaFolder(context.title);
var existing = [];
Eif (typeof implementation.getMedia === "function") {
var media = await implementation.getMedia(folder);
existing = (media || []).map(function (file) { return file.name; });
}
var dataFiles = (entry.dataFiles || []).map(function (file) {
return Object.assign({}, file);
});
for (var index = 0; index < assets.length; index += 1) {
var asset = assets[index];
Iif (!asset.fileObj) continue;
var cover = fieldName(asset) === "cover";
var processed = await DecapMediaProcessor.processFile(asset.fileObj, cover);
if (processed.size > 500 * 1024) {
var accepted = window.confirm(
processed.name + " 压缩后仍超过建议的 500KB,确定继续保存吗?",
);
if (!accepted) throw new Error("已取消大图保存。");
}
var extension = DecapMediaDomain.targetExtension(processed);
var name = DecapMediaDomain.nextFileName(existing, cover, extension);
existing.push(name);
var oldPath = asset.path;
var targetPath = folder + "/" + name;
var targetPublicPath =
"/images/posts/" + DecapMediaDomain.urlTitleSegment(context.title) + "/" + name;
var finalFile = new File([processed], name, {
type: processed.type,
lastModified: processed.lastModified || Date.now(),
});
asset.path = targetPath;
asset.fileObj = finalFile;
asset.url = window.URL.createObjectURL(finalFile);
dataFiles = dataFiles.map(function (dataFile) {
return Object.assign({}, dataFile, {
raw: rewriteReference(dataFile.raw, oldPath, targetPublicPath),
});
});
}
return Object.assign({}, entry, { dataFiles: dataFiles, assets: assets });
}
function rewriteEntry(entry, context) {
var targetPath = DecapEditorialDomain.articlePath(context.title);
var dataFiles = (entry.dataFiles || []).map(function (dataFile, index) {
if (index !== 0) return dataFile;
var rewritten = Object.assign({}, dataFile, { slug: context.title });
pathAliases[dataFile.path] = targetPath;
if (context.newRecord) {
rewritten.path = targetPath;
delete rewritten.newPath;
} else if (dataFile.path !== targetPath) {
rewritten.newPath = targetPath;
} else E{
delete rewritten.newPath;
}
return rewritten;
});
return Object.assign({}, entry, { dataFiles: dataFiles });
}
function wrapBackend(name) {
var registration = CMS.getBackend(name);
Iif (!registration || typeof registration.init !== "function" || registration.__editorialWrapped) {
return;
}
var originalInit = registration.init;
registration.__editorialWrapped = true;
registration.init = function () {
var implementation = originalInit.apply(this, arguments);
Iif (!implementation || implementation.__editorialWrapped) return implementation;
var originalPersist = implementation.persistEntry;
var originalGetEntry = implementation.getEntry;
implementation.__editorialWrapped = true;
implementation.__decapBackendName = name;
implementation.__persistEditorialTransaction = originalPersist.bind(implementation);
Eif (typeof originalGetEntry === "function") {
implementation.getEntry = function (path) {
return originalGetEntry.call(implementation, pathAliases[path] || path);
};
}
implementation.persistEntry = async function (entry, options) {
var context = consumeSaveContext();
Iif (!context.isPost) return originalPersist.call(implementation, entry, options);
var targetPath = DecapEditorialDomain.articlePath(context.title);
await assertUniqueTitle(implementation, context, targetPath);
var preparedEntry = await prepareAssets(implementation, entry, context);
var renamedEntry = await prepareRename(implementation, preparedEntry, context);
var rewrittenEntry = rewriteEntry(renamedEntry, context);
savedTitle = context.title;
window.DecapArticleMediaBackend = implementation;
var result = await originalPersist.call(implementation, rewrittenEntry, options);
if (
name === "proxy" &&
context.proxyOldMediaPaths &&
context.proxyOldMediaPaths.length &&
typeof implementation.deleteFiles === "function"
) {
await implementation.deleteFiles(
context.proxyOldMediaPaths,
"Remove old article media after rename",
);
}
return result;
};
window.DecapArticleMediaBackend = implementation;
return implementation;
};
}
CMS.registerEventListener({
name: "preSave",
handler: function (payload) {
var entry = payload && payload.entry;
var collection = entry && entry.get && entry.get("collection");
var isPost = collection === "posts";
var data = toPlainData(entry);
var title = typeof data.title === "string" ? data.title.trim() : "";
var currentPath = (entry && entry.get && entry.get("path")) || "";
var newRecord = Boolean(entry && entry.get && entry.get("newRecord"));
Eif (isPost) {
var validation = DecapEditorialDomain.validatePost(data);
var titleErrors = DecapEditorialDomain.validateTitle(data.title);
var errors = validation.errors.concat(titleErrors).filter(function (error, index, all) {
return all.indexOf(error) === index;
});
Iif (errors.length) throw new Error(errors.join("\n"));
var oldTitle = titleFromPath(currentPath);
if (!newRecord && oldTitle && oldTitle !== title) {
if (data.draft !== true) {
throw new Error("已发布文章不能直接修改标题;请先将文章改为草稿,再使用重命名文章。");
}
var accepted = window.confirm(
"确认重命名文章?旧链接将立即失效,外部链接、搜索记录和 Giscus 评论关联可能丢失,系统不会创建跳转。",
);
if (!accepted) throw new Error("已取消文章重命名。");
}
if (validation.warnings.length) {
var continueSave = window.confirm(
validation.warnings.join("\n") + "\n\n仍要继续保存吗?",
);
if (!continueSave) throw new Error("已取消保存。");
}
}
pendingSaveContext = {
isPost: isPost,
title: title,
currentPath: currentPath,
newRecord: newRecord,
};
},
});
CMS.registerEventListener({
name: "postSave",
handler: function () {
Eif (!savedTitle) return;
var title = savedTitle;
savedTitle = "";
window.setTimeout(function () {
window.location.hash = "#/collections/posts/entries/" + title;
}, 400);
},
});
["github", "proxy"].forEach(wrapBackend);
})();
|