mirror of
https://github.com/outline/outline.git
synced 2026-11-22 05:51:43 +03:00
Nodes are cached against the content or state they were parsed from, so repeated helper calls on the same document parse it once. JSON transform helpers now replace attrs instead of mutating them, as Node.toJSON shares attrs with the cached node.
31 lines
1014 B
TypeScript
31 lines
1014 B
TypeScript
import { serializer } from "@server/editor";
|
|
import { Document } from "@server/models";
|
|
import type { DocumentEvent } from "@server/types";
|
|
import { DocumentHelper } from "@server/models/helpers/DocumentHelper";
|
|
import { iso6393To1 } from "@shared/utils/language";
|
|
import { BaseTask } from "./base/BaseTask";
|
|
|
|
export default class DocumentUpdateTextTask extends BaseTask<DocumentEvent> {
|
|
public async perform(event: DocumentEvent) {
|
|
const document = await Document.findByPk(event.documentId);
|
|
if (!document?.content) {
|
|
return;
|
|
}
|
|
|
|
document.text = serializer.serialize(
|
|
DocumentHelper.toProsemirror(document)
|
|
);
|
|
|
|
// Loaded lazily to keep the language-detection corpus off the startup path —
|
|
// only this worker task needs it.
|
|
const { franc } = await import("franc");
|
|
|
|
const language = franc(DocumentHelper.toPlainText(document), {
|
|
minLength: 50,
|
|
});
|
|
document.language = iso6393To1(language) ?? null;
|
|
|
|
await document.save({ silent: true });
|
|
}
|
|
}
|