加入「拾编」写作台:文章 / 分类管理 + Markdown 上传

- 新增 studio 应用与 /manage/ 写作台
- 文章增删改查、状态/分类/标签、搜索筛选分页
- 分类增删改查,支持父分类
- 直接上传 .md(front matter)并创建文章,编辑页支持 Markdown 导入
- Post 增加 content_format,公开文章页支持 Markdown 渲染
- 复用拾匣密码解锁;首页页脚新增写作台入口
This commit is contained in:
2026-11-21 12:52:28 +08:00
parent d0f921ed27
commit 128dc20cd5
21 changed files with 2166 additions and 2 deletions
+721
View File
@@ -0,0 +1,721 @@
/* =========================================================
拾编 · 写作台
========================================================= */
body {
font-family: Inter, 'Noto Sans SC', -apple-system, 'PingFang SC', sans-serif;
}
main#article {
max-width: 1180px;
padding-top: clamp(84px, 11vh, 112px);
padding-bottom: 80px;
}
.read-progress,
.toc { display: none; }
.st {
max-width: 1120px;
margin: 0 auto;
}
/* ---------- 顶部 ---------- */
.st-head {
display: flex;
align-items: flex-end;
justify-content: space-between;
gap: 20px;
flex-wrap: wrap;
margin-bottom: 22px;
}
.st-eyebrow {
margin: 0 0 10px;
color: var(--ink-faint);
font-size: 10.5px;
font-weight: 500;
letter-spacing: .24em;
}
.st-eyebrow span { margin-right: 7px; color: var(--gold); font-size: 10px; }
.st-head h1 {
margin: 0 0 8px;
color: var(--ink);
font-family: 'Noto Serif SC', serif;
font-size: clamp(1.8rem, 3.6vw, 2.5rem);
font-weight: 900;
line-height: 1.3;
letter-spacing: .01em;
}
.st-sub { margin: 0; color: var(--ink-faint); font-size: 12.5px; line-height: 1.8; }
.st-head-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.st-back {
display: inline-flex;
align-items: center;
gap: 6px;
margin-bottom: 14px;
color: var(--ink-faint);
font-size: 12px;
text-decoration: none;
transition: color .2s ease;
}
.st-back:hover { color: var(--accent); }
/* ---------- 按钮 ---------- */
.st-btn {
display: inline-flex;
align-items: center;
justify-content: center;
gap: 6px;
min-height: 32px;
padding: 6px 14px;
border: 1px solid var(--line);
border-radius: 9px;
background: var(--paper);
color: var(--ink-soft);
font-family: inherit;
font-size: 12px;
line-height: 1;
text-decoration: none;
cursor: pointer;
transition: all .18s ease;
}
.st-btn:hover {
color: var(--accent);
border-color: color-mix(in srgb, var(--accent) 45%, transparent);
background: var(--accent-soft);
transform: translateY(-1px);
}
.st-btn-primary {
border-color: transparent;
background: linear-gradient(135deg, var(--accent), var(--gold));
color: #fff;
font-weight: 600;
box-shadow: 0 10px 22px -14px var(--accent);
}
.st-btn-primary:hover {
color: #fff;
border-color: transparent;
background: linear-gradient(135deg, var(--accent), var(--accent));
}
.st-btn-ghost { border-color: transparent; background: transparent; color: var(--ink-faint); }
.st-btn-ghost:hover { background: var(--paper-soft); }
.st-btn-danger,
.st-btn.st-danger {
border-color: color-mix(in srgb, var(--accent) 35%, var(--line));
background: var(--accent-soft);
color: var(--accent);
}
.st-btn.st-danger:hover { background: var(--accent); color: #fff; border-color: var(--accent); }
.st-link-danger {
padding: 0;
border: 0;
background: none;
color: var(--ink-faint);
font-family: inherit;
font-size: 11.5px;
cursor: pointer;
transition: color .18s ease;
}
.st-link-danger:hover { color: var(--accent); }
/* ---------- 数据卡 ---------- */
.st-stats {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(104px, 1fr));
gap: 10px;
margin-bottom: 18px;
}
.st-stat {
padding: 13px 16px;
border: 1px solid var(--line);
border-radius: 12px;
background: var(--paper);
box-shadow: var(--shadow-sm);
}
.st-stat b {
display: block;
color: var(--ink);
font-family: 'Noto Serif SC', serif;
font-size: 22px;
font-weight: 900;
line-height: 1.2;
}
.st-stat span { color: var(--ink-faint); font-size: 10.5px; letter-spacing: .04em; }
/* ---------- 工具条 / 上传 ---------- */
.st-toolbar {
display: flex;
align-items: center;
gap: 9px;
flex-wrap: wrap;
margin-bottom: 14px;
}
.st-toolbar-hint { color: var(--ink-faint); font-size: 10.5px; }
.st-upload-panel {
margin-bottom: 18px;
padding: 16px 18px;
border: 1px solid var(--line);
border-radius: 14px;
background: var(--paper);
box-shadow: var(--shadow-sm);
animation: st-rise .3s ease both;
}
.st-upload-panel[hidden] { display: none; }
.st-upload-grid {
display: grid;
grid-template-columns: minmax(0, 1.3fr) minmax(0, 1fr) minmax(0, .9fr) auto;
gap: 10px;
align-items: end;
}
.st-upload-grid label {
display: flex;
flex-direction: column;
gap: 5px;
color: var(--ink-faint);
font-size: 10.5px;
letter-spacing: .04em;
}
.st-file {
width: 100%;
padding: 6px 9px;
border: 1px dashed var(--line);
border-radius: 9px;
background: var(--paper-soft);
color: var(--ink-soft);
font-family: inherit;
font-size: 11.5px;
}
.st-help {
margin: 10px 0 0;
color: var(--ink-faint);
font-size: 10.5px;
line-height: 1.8;
}
.st-help code {
padding: 1px 4px;
border-radius: 4px;
background: var(--paper-soft);
border: 1px solid var(--line-soft);
font-family: 'JetBrains Mono', monospace;
font-size: 9.5px;
}
/* ---------- 布局 ---------- */
.st-layout {
display: grid;
grid-template-columns: minmax(0, 1fr) 320px;
gap: 18px;
align-items: start;
}
.st-panel {
padding: 18px 20px;
border: 1px solid var(--line);
border-radius: 16px;
background: var(--paper);
box-shadow: var(--shadow-sm);
}
.st-panel-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-bottom: 14px;
}
.st-panel-head h2 {
display: flex;
align-items: center;
gap: 8px;
margin: 0;
color: var(--ink);
font-family: 'Noto Serif SC', serif;
font-size: 15px;
font-weight: 700;
letter-spacing: .05em;
}
.st-panel-head h2 b {
padding: 1px 7px;
border-radius: 999px;
background: var(--paper-soft);
color: var(--ink-faint);
font-family: Inter, sans-serif;
font-size: 10px;
font-weight: 600;
}
.st-panel-head a { color: var(--accent); font-size: 11.5px; text-decoration: none; }
.st-panel-head a:hover { text-decoration: underline; }
/* ---------- 筛选 ---------- */
.st-filters {
display: flex;
gap: 7px;
flex-wrap: wrap;
margin-bottom: 12px;
}
.st-filters .st-input { width: auto; min-width: 110px; }
.st-filters input[type="search"] { flex: 1; min-width: 160px; }
/* ---------- 表格 ---------- */
.st-table-wrap { overflow-x: auto; margin: 0 -4px; padding: 0 4px; }
.st-table {
width: 100%;
min-width: 680px;
border-collapse: collapse;
font-size: 12px;
}
.st-table th {
padding: 8px 10px;
border-bottom: 1px solid var(--line);
color: var(--ink-faint);
font-size: 9.5px;
font-weight: 500;
letter-spacing: .12em;
text-align: left;
white-space: nowrap;
}
.st-table td {
padding: 11px 10px;
border-bottom: 1px solid var(--line-soft);
color: var(--ink-soft);
vertical-align: top;
}
.st-table tr:last-child td { border-bottom: 0; }
.st-table tr:hover td { background: color-mix(in srgb, var(--paper-soft) 55%, transparent); }
.st-post-title {
display: block;
color: var(--ink);
font-family: 'Noto Serif SC', serif;
font-size: 13.5px;
font-weight: 700;
line-height: 1.5;
text-decoration: none;
transition: color .18s ease;
}
.st-post-title:hover { color: var(--accent); }
.st-slug {
display: block;
margin-top: 2px;
color: var(--ink-faint);
font-family: 'JetBrains Mono', monospace;
font-size: 9.5px;
white-space: nowrap;
}
.st-badge {
display: inline-block;
padding: 3px 8px;
border-radius: 999px;
font-size: 9.5px;
font-weight: 600;
white-space: nowrap;
}
.st-badge.published { background: rgba(107, 143, 113, .14); color: #5d7f63; }
.st-badge.draft { background: rgba(209, 154, 63, .16); color: #9a7220; }
:root[data-theme="dark"] .st-badge.published { background: rgba(127, 174, 134, .16); color: #8fc096; }
:root[data-theme="dark"] .st-badge.draft { background: rgba(224, 176, 92, .16); color: #e0b05c; }
.st-cats { display: flex; gap: 4px; flex-wrap: wrap; }
.st-cats span {
padding: 2px 7px;
border: 1px solid var(--line);
border-radius: 999px;
color: var(--ink-faint);
font-size: 9.5px;
white-space: nowrap;
}
.st-cats i { color: var(--ink-faint); font-size: 9.5px; font-style: normal; }
.st-table time { color: var(--ink-faint); font-size: 10.5px; white-space: nowrap; }
.st-row-actions {
display: flex;
align-items: center;
gap: 8px;
white-space: nowrap;
}
.st-row-actions a {
color: var(--ink-faint);
font-size: 11.5px;
text-decoration: none;
transition: color .18s ease;
}
.st-row-actions a:hover { color: var(--accent); }
.st-row-actions form { display: inline; margin: 0; }
.st-table-empty {
padding: 40px 10px !important;
color: var(--ink-faint);
text-align: center;
}
.st-pager {
display: flex;
align-items: center;
justify-content: center;
gap: 14px;
margin-top: 14px;
color: var(--ink-faint);
font-size: 11px;
}
.st-pager a { color: var(--accent); text-decoration: none; }
.st-pager a:hover { text-decoration: underline; }
/* ---------- 分类面板 ---------- */
.st-cat-list { list-style: none; margin: 0 0 18px; padding: 0; }
.st-cat-list li {
display: flex;
align-items: center;
gap: 8px;
padding: 8px 9px;
border-bottom: 1px solid var(--line-soft);
border-radius: 8px;
}
.st-cat-list li:last-child { border-bottom: 0; }
.st-cat-list li.is-editing { background: var(--accent-soft); }
.st-cat-info { min-width: 0; flex: 1; }
.st-cat-info a {
display: block;
overflow: hidden;
color: var(--ink);
font-family: 'Noto Serif SC', serif;
font-size: 12.5px;
font-weight: 600;
text-decoration: none;
text-overflow: ellipsis;
white-space: nowrap;
}
.st-cat-info a:hover { color: var(--accent); }
.st-cat-info small {
display: block;
overflow: hidden;
margin-top: 1px;
color: var(--ink-faint);
font-family: 'JetBrains Mono', monospace;
font-size: 9px;
text-overflow: ellipsis;
white-space: nowrap;
}
.st-cat-count {
min-width: 22px;
padding: 1px 6px;
border-radius: 999px;
background: var(--paper-soft);
color: var(--ink-faint);
font-size: 9.5px;
text-align: center;
}
.st-cat-actions { display: flex; align-items: center; gap: 7px; }
.st-cat-actions a {
color: var(--ink-faint);
font-size: 10.5px;
text-decoration: none;
}
.st-cat-actions a:hover { color: var(--accent); }
.st-cat-actions form { display: inline; margin: 0; }
.st-cat-empty { color: var(--ink-faint); font-size: 11.5px; }
.st-cat-form-title {
margin: 0 0 10px;
padding-top: 14px;
border-top: 1px dashed var(--line);
color: var(--ink);
font-family: 'Noto Serif SC', serif;
font-size: 13px;
font-weight: 700;
}
.st-cat-form { display: flex; flex-direction: column; gap: 8px; }
.st-cat-form label,
.st-field label {
display: flex;
flex-direction: column;
gap: 4px;
color: var(--ink-faint);
font-size: 10.5px;
letter-spacing: .05em;
}
.st-cat-form .st-input,
.st-field .st-input { width: 100%; }
/* ---------- 输入 ---------- */
.st-input {
width: 100%;
padding: 7px 10px;
border: 1px solid var(--line);
border-radius: 8px;
background: var(--paper-soft);
color: var(--ink);
font-family: inherit;
font-size: 12px;
line-height: 1.65;
outline: none;
transition: border-color .18s ease, box-shadow .18s ease;
}
.st-input:focus {
border-color: color-mix(in srgb, var(--accent) 45%, transparent);
box-shadow: 0 0 0 3px var(--accent-soft);
}
.st-input[type="search"]:focus { box-shadow: 0 0 0 3px var(--accent-soft); }
textarea.st-input { resize: vertical; }
.st-form-actions {
display: flex;
align-items: center;
gap: 8px;
flex-wrap: wrap;
margin-top: 6px;
}
/* ---------- 消息 / 错误 ---------- */
.st-messages { display: flex; flex-direction: column; gap: 7px; margin-bottom: 14px; }
.st-msg {
margin: 0;
padding: 8px 12px;
border: 1px solid var(--line);
border-left: 3px solid var(--ink-faint);
border-radius: 8px;
background: var(--paper);
color: var(--ink-soft);
font-size: 11.5px;
line-height: 1.7;
}
.st-msg.success { border-left-color: #6b8f71; }
.st-msg.info { border-left-color: var(--c4); }
.st-msg.warning { border-left-color: var(--gold); }
.st-msg.error { border-left-color: var(--accent); color: var(--accent); }
.st .errorlist {
margin: 4px 0 0;
padding: 0;
list-style: none;
color: var(--accent);
font-size: 10.5px;
line-height: 1.7;
}
.st .errorlist li { margin: 0; }
/* ---------- 编辑器 ---------- */
.st-editor {
display: grid;
grid-template-columns: minmax(0, 1fr) 330px;
gap: 18px;
align-items: start;
}
.st-editor-main,
.st-side-card {
border: 1px solid var(--line);
border-radius: 16px;
background: var(--paper);
box-shadow: var(--shadow-sm);
}
.st-editor-main { padding: 20px 22px; }
.st-side-card { padding: 16px 18px; }
.st-editor-side { display: flex; flex-direction: column; gap: 14px; }
.st-field { margin-bottom: 14px; }
.st-field:last-child { margin-bottom: 0; }
.st-title-input {
padding: 10px 12px;
font-family: 'Noto Serif SC', serif;
font-size: 1.35rem;
font-weight: 700;
line-height: 1.5;
}
.st-editor-meta-row {
display: grid;
grid-template-columns: minmax(0, 1fr) 150px;
gap: 14px;
}
.st-content-head {
display: flex;
align-items: center;
gap: 12px;
flex-wrap: wrap;
margin-bottom: 6px;
}
.st-content-head > label {
color: var(--ink-faint);
font-size: 10.5px;
letter-spacing: .05em;
}
.st-md-import {
position: relative;
display: inline-flex;
align-items: center;
padding: 3px 9px;
border: 1px dashed var(--line);
border-radius: 999px;
color: var(--ink-faint);
font-size: 10px;
cursor: pointer;
transition: all .18s ease;
}
.st-md-import:hover { color: var(--accent); border-color: var(--accent); background: var(--accent-soft); }
.st-md-import input {
position: absolute;
width: 1px; height: 1px;
opacity: 0;
pointer-events: none;
}
.st-char-count { margin-left: auto; color: var(--ink-faint); font-size: 10px; font-variant-numeric: tabular-nums; }
.st-char-count b { color: var(--ink-soft); font-weight: 600; }
.st-content {
min-height: 58vh;
padding: 14px 16px;
border: 1px solid var(--line);
border-radius: 10px;
background: var(--paper-soft);
color: var(--ink);
font-family: 'JetBrains Mono', 'Noto Serif SC', monospace;
font-size: 12.5px;
line-height: 1.85;
resize: vertical;
tab-size: 2;
}
.st-side-card h2 {
margin: 0 0 12px;
padding-bottom: 8px;
border-bottom: 1px dashed var(--line);
color: var(--ink);
font-family: 'Noto Serif SC', serif;
font-size: 13px;
font-weight: 700;
letter-spacing: .06em;
}
.st-side-card .st-field { margin-bottom: 10px; }
.st-side-card .st-form-actions { margin-top: 10px; }
.st-side-card .st-help { margin-top: 8px; }
.st-check-grid {
display: flex;
flex-wrap: wrap;
gap: 5px 10px;
max-height: 190px;
overflow-y: auto;
padding-right: 4px;
}
.st-check {
display: inline-flex;
align-items: center;
gap: 5px;
color: var(--ink-soft);
font-size: 11px;
cursor: pointer;
}
.st-check input { accent-color: var(--accent); }
.st-check span { line-height: 1.6; }
.st-danger-zone { border-color: color-mix(in srgb, var(--accent) 22%, var(--line)); }
/* ---------- 锁定页 ---------- */
.st-lock-page {
display: grid;
place-items: center;
min-height: calc(100vh - 240px);
padding: 20px 0 60px;
}
.st-lock-card {
width: min(92vw, 410px);
padding: 34px 34px 28px;
border: 1px solid var(--line);
border-radius: 22px;
background: var(--paper);
box-shadow: var(--shadow);
text-align: center;
}
.st-lock-card h1 {
margin: 0 0 8px;
color: var(--ink);
font-family: 'Noto Serif SC', serif;
font-size: 1.9rem;
font-weight: 900;
}
.st-lock-sub { margin: 0 0 20px; color: var(--ink-faint); font-size: 12px; line-height: 1.8; }
.st-unlock-form { display: flex; gap: 7px; }
.st-unlock-form input {
flex: 1;
min-width: 0;
padding: 8px 11px;
border: 1px solid var(--line);
border-radius: 9px;
background: var(--paper-soft);
color: var(--ink);
font-family: inherit;
font-size: 12px;
outline: none;
}
.st-unlock-form input:focus {
border-color: color-mix(in srgb, var(--accent) 45%, transparent);
box-shadow: 0 0 0 3px var(--accent-soft);
}
.st-unlock-form .st-btn { flex: none; }
.st-error {
margin: 12px 0 0;
padding: 8px 10px;
border: 1px solid color-mix(in srgb, var(--accent) 30%, var(--line));
border-radius: 8px;
background: var(--accent-soft);
color: var(--accent);
font-size: 11.5px;
line-height: 1.7;
}
.st-lock-hint { margin: 16px 0 0; color: var(--ink-faint); font-size: 10px; }
.st-note {
margin: 26px 0 0;
padding-top: 14px;
border-top: 1px dashed var(--line);
color: var(--ink-faint);
font-size: 10.5px;
line-height: 1.9;
}
@keyframes st-rise {
from { opacity: 0; transform: translateY(8px); }
to { opacity: 1; transform: none; }
}
/* ---------- 响应式 ---------- */
@media (max-width: 980px) {
.st-layout,
.st-editor { grid-template-columns: 1fr; }
.st-editor-side { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); }
}
@media (max-width: 720px) {
.st-head { align-items: flex-start; }
.st-head-actions { width: 100%; }
.st-stats { grid-template-columns: repeat(3, 1fr); }
.st-stat { padding: 10px 12px; }
.st-stat b { font-size: 18px; }
.st-upload-grid { grid-template-columns: 1fr; }
.st-filters .st-input { flex: 1; min-width: 0; }
.st-editor-main { padding: 16px; }
.st-editor-meta-row { grid-template-columns: 1fr; }
.st-panel { padding: 14px; }
.st-content { min-height: 48vh; }
}
@media (max-width: 420px) {
.st-stats { grid-template-columns: repeat(2, 1fr); }
.st-unlock-form { flex-direction: column; }
.st-unlock-form .st-btn { width: 100%; }
}
@media (prefers-reduced-motion: reduce) {
.st-upload-panel { animation: none; }
.st-btn { transition: none; }
}
/* JS 临时提示 */
.st-js-toast {
position: fixed;
left: 50%;
bottom: 32px;
z-index: 1100;
max-width: min(90vw, 460px);
padding: 9px 16px;
border-radius: 10px;
background: var(--ink);
color: var(--bg);
font-size: 12px;
line-height: 1.7;
box-shadow: var(--shadow);
opacity: 0;
pointer-events: none;
transform: translate(-50%, 8px);
transition: opacity .2s ease, transform .2s ease;
}
.st-js-toast.is-visible { opacity: 1; transform: translate(-50%, 0); }
+243
View File
@@ -0,0 +1,243 @@
/* =========================================================
拾编 · 写作台交互
========================================================= */
(function () {
'use strict';
var csrfMeta = document.querySelector('meta[name="csrf-token"]');
var CSRF = csrfMeta ? csrfMeta.getAttribute('content') || '' : '';
var $ = function (selector) { return document.querySelector(selector); };
function toast(message) {
var box = document.querySelector('.st-js-toast');
if (!box) {
box = document.createElement('div');
box.className = 'st-js-toast';
document.body.appendChild(box);
}
box.textContent = message;
box.classList.add('is-visible');
clearTimeout(box._timer);
box._timer = setTimeout(function () {
box.classList.remove('is-visible');
}, 3200);
}
function showError(message) {
var box = $('#stError');
if (box) {
box.textContent = message;
box.hidden = false;
return;
}
toast(message);
}
function hideError() {
var box = $('#stError');
if (box) box.hidden = true;
}
async function postJson(url, payload) {
var response = await fetch(url, {
method: 'POST',
credentials: 'same-origin',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'X-CSRFToken': CSRF
},
body: JSON.stringify(payload || {})
});
var data = null;
try { data = await response.json(); } catch (err) {}
if (!response.ok) {
var error = new Error((data && data.error) || ('请求失败(HTTP ' + response.status + ')'));
error.code = data && data.code;
throw error;
}
return data || {};
}
/* ---------- 解锁 / 锁定 ---------- */
var unlockForm = $('#stUnlockForm');
if (unlockForm) {
unlockForm.addEventListener('submit', async function (event) {
event.preventDefault();
var input = $('#stPassword');
var button = unlockForm.querySelector('button[type="submit"]');
var password = input ? input.value : '';
if (!password) {
showError('请输入密码');
if (input) input.focus();
return;
}
button.disabled = true;
hideError();
try {
await postJson('/files/unlock/', { password: password });
window.location.reload();
} catch (error) {
showError((error && error.message) || '解锁失败');
button.disabled = false;
if (input) input.select();
}
});
}
var lockBtn = $('#stLockBtn');
if (lockBtn) {
lockBtn.addEventListener('click', async function () {
lockBtn.disabled = true;
try {
await postJson('/files/lock/', {});
window.location.reload();
} catch (error) {
toast((error && error.message) || '锁定失败');
lockBtn.disabled = false;
}
});
}
/* ---------- 上传 Markdown 面板 ---------- */
var uploadToggle = $('#stUploadToggle');
var uploadForm = $('#stUploadForm');
if (uploadToggle && uploadForm) {
uploadToggle.addEventListener('click', function () {
uploadForm.hidden = !uploadForm.hidden;
if (!uploadForm.hidden && uploadForm.scrollIntoView) {
uploadForm.scrollIntoView({ behavior: 'smooth', block: 'center' });
}
});
}
/* ---------- 删除确认 ---------- */
Array.prototype.forEach.call(document.querySelectorAll('form[data-confirm]'), function (form) {
form.addEventListener('submit', function (event) {
if (!window.confirm(form.getAttribute('data-confirm') || '确定执行这个操作?')) {
event.preventDefault();
}
});
});
/* ---------- 编辑器 ---------- */
var content = $('#id_content');
var counter = $('#stCharCount');
if (content && counter) {
var updateCount = function () {
counter.textContent = String(content.value.length);
};
content.addEventListener('input', updateCount);
updateCount();
}
var importer = $('#stMdImport');
if (importer && content) {
importer.addEventListener('change', function () {
var file = importer.files && importer.files[0];
if (!file) return;
if (file.size > 2 * 1024 * 1024) {
showError('Markdown 文件不能超过 2 MB');
importer.value = '';
return;
}
var reader = new FileReader();
reader.onload = function () {
var text = String(reader.result || '');
var parsed = splitFrontMatter(text);
var meta = parsed[0];
var body = parsed[1];
var metaTitle = (meta.title || '').trim();
var heading = firstHeading(body);
var title = metaTitle || heading || file.name.replace(/\.(md|markdown)$/i, '');
if (metaTitle) {
if (heading && heading === metaTitle) body = removeFirstHeading(body);
} else if (heading) {
body = removeFirstHeading(body);
}
if (content.value.trim() && !window.confirm('导入会覆盖当前正文,继续?')) {
importer.value = '';
return;
}
setValue('#id_content', body.trim());
setValue('#id_title', title);
setValue('#id_slug', meta.slug || '');
setValue('#id_excerpt', meta.excerpt || '');
setValue('#id_content_format', 'markdown');
if (meta.status === 'draft' || meta.status === 'published') {
setValue('#id_status', meta.status);
}
if (meta.date) {
var match = String(meta.date).match(/^(\d{4})-(\d{2})-(\d{2})(?:[T ](\d{2}):(\d{2}))?/);
if (match) {
setValue(
'#id_published_at',
match[1] + '-' + match[2] + '-' + match[3] + 'T' + (match[4] || '00') + ':' + (match[5] || '00')
);
}
}
if (counter) counter.textContent = String(content.value.length);
hideError();
toast('已导入 Markdown,检查后保存');
importer.value = '';
};
reader.onerror = function () {
showError('读取文件失败');
importer.value = '';
};
reader.readAsText(file, 'utf-8');
});
}
function setValue(selector, value) {
var element = $(selector);
if (element) element.value = value;
}
function splitFrontMatter(text) {
text = text.replace(/^\uFEFF/, '');
var lines = text.split(/\r?\n/);
if (!lines.length || lines[0].trim() !== '---') return [{}, text];
var end = -1;
for (var i = 1; i < lines.length; i++) {
if (lines[i].trim() === '---' || lines[i].trim() === '...') {
end = i;
break;
}
}
if (end < 0) return [{}, text];
var meta = {};
for (var j = 1; j < end; j++) {
var position = lines[j].indexOf(':');
if (position <= 0) continue;
var key = lines[j].slice(0, position).trim().toLowerCase();
var value = lines[j].slice(position + 1).trim().replace(/^['"]|['"]$/g, '');
if (key) meta[key] = value;
}
return [meta, lines.slice(end + 1).join('\n').replace(/^\n+/, '')];
}
function firstHeading(body) {
var match = String(body).match(/^#\s+(.+?)\s*$/m);
return match ? match[1].replace(/[*_`]+/g, '').trim() : '';
}
function removeFirstHeading(body) {
return String(body).replace(/^#\s+.+?\s*$/m, '').replace(/^\n+/, '');
}
})();