feat: refactor doc write in native (#14272)

This commit is contained in:
DarkSky
2026-01-18 16:31:12 +08:00
committed by GitHub
parent 753b11deeb
commit f373e08583
52 changed files with 7140 additions and 3559 deletions

View File

@@ -27,23 +27,24 @@ export interface Chunk {
content: string
}
export declare function fromModelName(modelName: string): Tokenizer | null
export declare function getMime(input: Uint8Array): string
export declare function htmlSanitize(input: string): string
/**
* Converts markdown content to AFFiNE-compatible y-octo document binary.
*
* # Arguments
* * `title` - The document title
* * `markdown` - The markdown content to convert
* * `doc_id` - The document ID to use for the y-octo doc
*
* # Returns
* A Buffer containing the y-octo document update binary
*/
export declare function markdownToDocBinary(markdown: string, docId: string): Buffer
export declare function createDocWithMarkdown(title: string, markdown: string, docId: string): Buffer
export declare function fromModelName(modelName: string): Tokenizer | null
export declare function getMime(input: Uint8Array): string
export declare function htmlSanitize(input: string): string
/**
* Merge updates in form like `Y.applyUpdate(doc, update)` way and return the
@@ -103,9 +104,38 @@ export declare function parseWorkspaceDoc(docBin: Buffer): NativeWorkspaceDocCon
export declare function readAllDocIdsFromRootDoc(docBin: Buffer, includeTrash?: boolean | undefined | null): Array<string>
/**
* Updates or creates the docProperties record for a document.
*
* # Arguments
* * `existing_binary` - The current docProperties document binary
* * `properties_doc_id` - The docProperties document ID
* (db$${workspaceId}$docProperties)
* * `target_doc_id` - The document ID to update in docProperties
* * `created_by` - Optional creator user ID
* * `updated_by` - Optional updater user ID
*
* # Returns
* A Buffer containing only the delta (changes) as a y-octo update binary
*/
export declare function updateDocProperties(existingBinary: Buffer, propertiesDocId: string, targetDocId: string, createdBy?: string | undefined | null, updatedBy?: string | undefined | null): Buffer
/**
* Updates a document's title without touching content blocks.
*
* # Arguments
* * `existing_binary` - The current document binary
* * `title` - The new title
* * `doc_id` - The document ID
*
* # Returns
* A Buffer containing only the delta (changes) as a y-octo update binary
*/
export declare function updateDocTitle(existingBinary: Buffer, title: string, docId: string): Buffer
/**
* Updates an existing document with new markdown content.
* Uses structural and text-level diffing to apply minimal changes.
* Uses structural diffing to apply block-level replacements for changes.
*
* # Arguments
* * `existing_binary` - The current document binary
@@ -117,4 +147,17 @@ export declare function readAllDocIdsFromRootDoc(docBin: Buffer, includeTrash?:
*/
export declare function updateDocWithMarkdown(existingBinary: Buffer, newMarkdown: string, docId: string): Buffer
/**
* Updates a document title in the workspace root doc's meta.pages array.
*
* # Arguments
* * `root_doc_bin` - The current root doc binary (workspaceId doc)
* * `doc_id` - The document ID to update
* * `title` - The new title for the document
*
* # Returns
* A Buffer containing the y-octo update binary to apply to the root doc
*/
export declare function updateRootDocMetaTitle(rootDocBin: Buffer, docId: string, title: string): Buffer
export declare function verifyChallengeResponse(response: string, bits: number, resource: string): Promise<boolean>

View File

@@ -136,20 +136,21 @@ pub fn read_all_doc_ids_from_root_doc(doc_bin: Buffer, include_trash: Option<boo
/// Converts markdown content to AFFiNE-compatible y-octo document binary.
///
/// # Arguments
/// * `title` - The document title
/// * `markdown` - The markdown content to convert
/// * `doc_id` - The document ID to use for the y-octo doc
///
/// # Returns
/// A Buffer containing the y-octo document update binary
#[napi]
pub fn markdown_to_doc_binary(markdown: String, doc_id: String) -> Result<Buffer> {
let result =
doc_parser::markdown_to_ydoc(&markdown, &doc_id).map_err(|e| Error::new(Status::GenericFailure, e.to_string()))?;
pub fn create_doc_with_markdown(title: String, markdown: String, doc_id: String) -> Result<Buffer> {
let result = doc_parser::build_full_doc(&title, &markdown, &doc_id)
.map_err(|e| Error::new(Status::GenericFailure, e.to_string()))?;
Ok(Buffer::from(result))
}
/// Updates an existing document with new markdown content.
/// Uses structural and text-level diffing to apply minimal changes.
/// Uses structural diffing to apply block-level replacements for changes.
///
/// # Arguments
/// * `existing_binary` - The current document binary
@@ -160,11 +161,58 @@ pub fn markdown_to_doc_binary(markdown: String, doc_id: String) -> Result<Buffer
/// A Buffer containing only the delta (changes) as a y-octo update binary
#[napi]
pub fn update_doc_with_markdown(existing_binary: Buffer, new_markdown: String, doc_id: String) -> Result<Buffer> {
let result = doc_parser::update_ydoc(&existing_binary, &new_markdown, &doc_id)
let result = doc_parser::update_doc(&existing_binary, &new_markdown, &doc_id)
.map_err(|e| Error::new(Status::GenericFailure, e.to_string()))?;
Ok(Buffer::from(result))
}
/// Updates a document's title without touching content blocks.
///
/// # Arguments
/// * `existing_binary` - The current document binary
/// * `title` - The new title
/// * `doc_id` - The document ID
///
/// # Returns
/// A Buffer containing only the delta (changes) as a y-octo update binary
#[napi]
pub fn update_doc_title(existing_binary: Buffer, title: String, doc_id: String) -> Result<Buffer> {
let result = doc_parser::update_doc_title(&existing_binary, &doc_id, &title)
.map_err(|e| Error::new(Status::GenericFailure, e.to_string()))?;
Ok(Buffer::from(result))
}
/// Updates or creates the docProperties record for a document.
///
/// # Arguments
/// * `existing_binary` - The current docProperties document binary
/// * `properties_doc_id` - The docProperties document ID
/// (db$${workspaceId}$docProperties)
/// * `target_doc_id` - The document ID to update in docProperties
/// * `created_by` - Optional creator user ID
/// * `updated_by` - Optional updater user ID
///
/// # Returns
/// A Buffer containing only the delta (changes) as a y-octo update binary
#[napi]
pub fn update_doc_properties(
existing_binary: Buffer,
properties_doc_id: String,
target_doc_id: String,
created_by: Option<String>,
updated_by: Option<String>,
) -> Result<Buffer> {
let result = doc_parser::update_doc_properties(
&existing_binary,
&properties_doc_id,
&target_doc_id,
created_by.as_deref(),
updated_by.as_deref(),
)
.map_err(|e| Error::new(Status::GenericFailure, e.to_string()))?;
Ok(Buffer::from(result))
}
/// Adds a document ID to the workspace root doc's meta.pages array.
/// This registers the document in the workspace so it appears in the UI.
///
@@ -181,3 +229,19 @@ pub fn add_doc_to_root_doc(root_doc_bin: Buffer, doc_id: String, title: Option<S
.map_err(|e| Error::new(Status::GenericFailure, e.to_string()))?;
Ok(Buffer::from(result))
}
/// Updates a document title in the workspace root doc's meta.pages array.
///
/// # Arguments
/// * `root_doc_bin` - The current root doc binary (workspaceId doc)
/// * `doc_id` - The document ID to update
/// * `title` - The new title for the document
///
/// # Returns
/// A Buffer containing the y-octo update binary to apply to the root doc
#[napi]
pub fn update_root_doc_meta_title(root_doc_bin: Buffer, doc_id: String, title: String) -> Result<Buffer> {
let result = doc_parser::update_root_doc_meta_title(&root_doc_bin, &doc_id, &title)
.map_err(|e| Error::new(Status::GenericFailure, e.to_string()))?;
Ok(Buffer::from(result))
}