mirror of
https://github.com/docmost/docmost.git
synced 2026-05-18 07:24:04 +08:00
dry
This commit is contained in:
@@ -100,30 +100,10 @@ export class PageService {
|
|||||||
let ydoc = undefined;
|
let ydoc = undefined;
|
||||||
|
|
||||||
if (createPageDto?.content && createPageDto?.format) {
|
if (createPageDto?.content && createPageDto?.format) {
|
||||||
let prosemirrorJson: any;
|
const prosemirrorJson = await this.parseProsemirrorContent(
|
||||||
|
createPageDto.content,
|
||||||
switch (createPageDto.format) {
|
createPageDto.format,
|
||||||
case 'markdown': {
|
);
|
||||||
const html = await markdownToHtml(createPageDto.content as string);
|
|
||||||
prosemirrorJson = htmlToJson(html as string);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 'html': {
|
|
||||||
prosemirrorJson = htmlToJson(createPageDto.content as string);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 'json':
|
|
||||||
default: {
|
|
||||||
prosemirrorJson = createPageDto.content;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
jsonToNode(prosemirrorJson);
|
|
||||||
} catch (err) {
|
|
||||||
throw new BadRequestException('Invalid content format');
|
|
||||||
}
|
|
||||||
|
|
||||||
content = prosemirrorJson;
|
content = prosemirrorJson;
|
||||||
textContent = jsonToText(prosemirrorJson);
|
textContent = jsonToText(prosemirrorJson);
|
||||||
@@ -240,30 +220,7 @@ export class PageService {
|
|||||||
format: ContentFormat,
|
format: ContentFormat,
|
||||||
user: User,
|
user: User,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
let prosemirrorJson: any;
|
const prosemirrorJson = await this.parseProsemirrorContent(content, format);
|
||||||
|
|
||||||
switch (format) {
|
|
||||||
case 'markdown': {
|
|
||||||
const html = await markdownToHtml(content as string);
|
|
||||||
prosemirrorJson = htmlToJson(html as string);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 'html': {
|
|
||||||
prosemirrorJson = htmlToJson(content as string);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 'json':
|
|
||||||
default: {
|
|
||||||
prosemirrorJson = content;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
jsonToNode(prosemirrorJson);
|
|
||||||
} catch (err) {
|
|
||||||
throw new BadRequestException('Invalid content');
|
|
||||||
}
|
|
||||||
|
|
||||||
const documentName = `page.${pageId}`;
|
const documentName = `page.${pageId}`;
|
||||||
await this.collaborationGateway.handleYjsEvent(
|
await this.collaborationGateway.handleYjsEvent(
|
||||||
@@ -754,4 +711,36 @@ export class PageService {
|
|||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
await this.pageRepo.removePage(pageId, userId, workspaceId);
|
await this.pageRepo.removePage(pageId, userId, workspaceId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async parseProsemirrorContent(
|
||||||
|
content: string | object,
|
||||||
|
format: ContentFormat,
|
||||||
|
): Promise<any> {
|
||||||
|
let prosemirrorJson: any;
|
||||||
|
|
||||||
|
switch (format) {
|
||||||
|
case 'markdown': {
|
||||||
|
const html = await markdownToHtml(content as string);
|
||||||
|
prosemirrorJson = htmlToJson(html as string);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'html': {
|
||||||
|
prosemirrorJson = htmlToJson(content as string);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'json':
|
||||||
|
default: {
|
||||||
|
prosemirrorJson = content;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
jsonToNode(prosemirrorJson);
|
||||||
|
} catch (err) {
|
||||||
|
throw new BadRequestException('Invalid content format');
|
||||||
|
}
|
||||||
|
|
||||||
|
return prosemirrorJson;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user