Import a Word document as a blog post, and add table blocks

- Table content blocks (migration 044): a grid of text, a row per
  line, cells split by |, the first row the header; cells take the
  same formatting as a paragraph; wide tables scroll on a phone
- Blog posts -> Import from Word: the server reads the .docx (zip and
  XML read without a library, sizes capped) and creates an unpublished
  post through the CRUD engine: title, date, headings, paragraphs,
  lists, tables, bold, italic and links; the editor opens with a note
  of what came over and what didn't (images)
- docs/importing.md, and an nginx block for the upload

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Zaldimmar 2026-09-29 12:44:20 -05:00
parent adc88e5714
commit 7f12824c93
16 changed files with 656 additions and 6 deletions

View file

@ -48,7 +48,7 @@
import { ArrowLeft } from "@phosphor-icons/react";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { useNavigate, useParams } from "react-router-dom";
import { useLocation, useNavigate, useParams } from "react-router-dom";
import { get, post, patch, del, ApiError } from "../../lib/api.ts";
import { isUnauthorized, useAuth } from "../../lib/auth.tsx";
@ -367,6 +367,8 @@ export default function EntityEdit() {
{isNew ? `New ${manifest.singular}` : heading}
</h1>
<ImportedNote />
{/* Straight in the page, not wrapped: a sticky element only
travels within its parent, so its parent has to be tall. */}
<SectionJump sections={sections} />
@ -527,3 +529,25 @@ export default function EntityEdit() {
</div>
);
}
/* After Import from Word (EntityList's ImportButton): what came over,
and what's left to do before publishing. Gone on the next visit. */
function ImportedNote() {
const summary = (useLocation().state as any)?.imported;
if (!summary) return null;
const parts = [
`${summary.headings} headings`,
`${summary.paragraphs} paragraphs`,
`${summary.lists} lists`,
`${summary.tables} tables`,
];
return (
<p role="status" className="mt-4 rounded-xl border border-brand/20 bg-tint px-4 py-3 text-sm text-body">
Imported from Word: {parts.join(", ")}.
{summary.skipped?.images
? ` ${summary.skipped.images} image${summary.skipped.images === 1 ? " was" : "s were"} not carried over; add them as Image blocks.`
: ""}{" "}
It's saved unpublished: check it, add the authors and a cover, then tick Published.
</p>
);
}

View file

@ -15,7 +15,7 @@
import { useCallback, useEffect, useState } from "react";
import { Link, Navigate, useNavigate, useParams, useSearchParams } from "react-router-dom";
import { get, ApiError } from "../../lib/api.ts";
import { get, upload, ApiError } from "../../lib/api.ts";
import { isUnauthorized, useAuth } from "../../lib/auth.tsx";
import { ADMIN_ENTITIES } from "../../lib/adminSchema.ts";
import { atLeast } from "../../lib/roles.ts";
@ -93,10 +93,11 @@ export default function EntityList() {
<div>
<div className="flex flex-wrap items-center gap-4">
<h1 className="text-3xl font-bold text-link">{manifest.label}</h1>
{canWrite && manifest.importer && <ImportButton manifest={manifest} />}
{canWrite && (
<Link
to={`/admin/${manifest.key}/new`}
className="ml-auto rounded-full bg-action px-4 py-2 text-sm font-semibold text-white transition-colors hover:bg-action-hover"
className={`${manifest.importer ? "" : "ml-auto "}rounded-full bg-action px-4 py-2 text-sm font-semibold text-white transition-colors hover:bg-action-hover`}
>
New {manifest.singular}
</Link>
@ -213,3 +214,54 @@ export default function EntityList() {
</div>
);
}
/* A manifest's `importer`: pick a file, the server turns it into a
new record, and its editor opens with a note of what came over
(EntityEdit shows it). Blog posts take Word documents. */
function ImportButton({ manifest }: { manifest: any }) {
const navigate = useNavigate();
const [busy, setBusy] = useState(false);
const [error, setError] = useState<string | null>(null);
async function send(file: File) {
setBusy(true);
setError(null);
const form = new FormData();
form.set("file", file);
try {
const result = await upload(manifest.importer.endpoint, form);
navigate(`/admin/${manifest.key}/${result.id}`, { state: { imported: result.summary } });
} catch (err) {
setError(err instanceof ApiError ? err.message : "Couldn't reach the server.");
setBusy(false);
}
}
return (
<div className="ml-auto flex items-center gap-3">
{error && (
<span role="alert" className="text-sm text-danger">
{error}
</span>
)}
<label
className={`cursor-pointer rounded-full border border-link px-4 py-2 text-sm font-semibold text-link transition-colors hover:bg-tint ${
busy ? "pointer-events-none opacity-60" : ""
}`}
>
{busy ? "Importing…" : manifest.importer.label}
<input
type="file"
accept={manifest.importer.accept}
className="sr-only"
disabled={busy}
onChange={(e) => {
const file = e.target.files?.[0];
e.target.value = "";
if (file) send(file);
}}
/>
</label>
</div>
);
}