mirror of
https://github.com/docmost/docmost.git
synced 2026-08-29 18:14:58 +08:00
fix: file import page title
This commit is contained in:
@@ -102,8 +102,10 @@ export class ImportService {
|
|||||||
throw new BadRequestException(message);
|
throw new BadRequestException(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
const { title, prosemirrorJson } =
|
const { title, prosemirrorJson } = this.extractTitleAndRemoveHeading(
|
||||||
this.extractTitleAndRemoveHeading(prosemirrorState);
|
prosemirrorState,
|
||||||
|
{ anyHeadingLevel: true },
|
||||||
|
);
|
||||||
|
|
||||||
const pageTitle = title || fileName;
|
const pageTitle = title || fileName;
|
||||||
|
|
||||||
@@ -246,18 +248,29 @@ export class ImportService {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
extractTitleAndRemoveHeading(prosemirrorState: any) {
|
extractTitleAndRemoveHeading(
|
||||||
|
prosemirrorState: any,
|
||||||
|
opts?: { anyHeadingLevel?: boolean },
|
||||||
|
) {
|
||||||
let title: string | null = null;
|
let title: string | null = null;
|
||||||
|
|
||||||
const content = prosemirrorState.content ?? [];
|
const content = prosemirrorState.content ?? [];
|
||||||
|
const firstNode = content[0];
|
||||||
|
|
||||||
if (
|
const isTitleHeading =
|
||||||
content.length > 0 &&
|
firstNode?.type === 'heading' &&
|
||||||
content[0].type === 'heading' &&
|
(opts?.anyHeadingLevel || firstNode.attrs?.level === 1);
|
||||||
content[0].attrs?.level === 1
|
|
||||||
) {
|
if (isTitleHeading) {
|
||||||
title = content[0].content?.[0]?.text ?? null;
|
const headingText = (firstNode.content ?? [])
|
||||||
content.shift();
|
.map((node: any) => node.text ?? '')
|
||||||
|
.join('')
|
||||||
|
.trim();
|
||||||
|
|
||||||
|
if (headingText) {
|
||||||
|
title = headingText;
|
||||||
|
content.shift();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ensure at least one paragraph
|
// ensure at least one paragraph
|
||||||
|
|||||||
Reference in New Issue
Block a user