mirror of
https://github.com/vrtmrz/obsidian-livesync.git
synced 2026-10-03 16:02:29 +00:00
Preserve UTF-8 BOMs during Vault ingestion
This commit is contained in:
@@ -10,7 +10,8 @@ export class ObsidianVaultAdapter implements IVaultAdapter<TFile, TFolder> {
|
|||||||
constructor(private app: App) {}
|
constructor(private app: App) {}
|
||||||
|
|
||||||
async read(file: TFile): Promise<string> {
|
async read(file: TFile): Promise<string> {
|
||||||
return await this.app.vault.read(file);
|
// Vault.read strips a leading UTF-8 BOM, leaving the content size inconsistent with TFile.stat.
|
||||||
|
return await this.app.vault.adapter.read(file.path);
|
||||||
}
|
}
|
||||||
|
|
||||||
async cachedRead(file: TFile): Promise<string> {
|
async cachedRead(file: TFile): Promise<string> {
|
||||||
|
|||||||
@@ -0,0 +1,37 @@
|
|||||||
|
import { describe, expect, it, vi } from "vitest";
|
||||||
|
import type { App, TFile } from "obsidian";
|
||||||
|
import { ObsidianVaultAdapter } from "./ObsidianVaultAdapter";
|
||||||
|
|
||||||
|
describe("ObsidianVaultAdapter.read", () => {
|
||||||
|
it("preserves a UTF-8 BOM so the content size matches the file stat", async () => {
|
||||||
|
const path = "Transcripts/字幕.md";
|
||||||
|
const contentWithoutBom = "字幕の検証行です。\n";
|
||||||
|
const contentWithBom = `\ufeff${contentWithoutBom}`;
|
||||||
|
const read = vi.fn().mockResolvedValue(contentWithoutBom);
|
||||||
|
const adapterRead = vi.fn().mockResolvedValue(contentWithBom);
|
||||||
|
const app = {
|
||||||
|
vault: {
|
||||||
|
read,
|
||||||
|
adapter: {
|
||||||
|
read: adapterRead,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
} as unknown as App;
|
||||||
|
const file = {
|
||||||
|
path,
|
||||||
|
stat: {
|
||||||
|
ctime: 1,
|
||||||
|
mtime: 2,
|
||||||
|
size: new Blob([contentWithBom]).size,
|
||||||
|
},
|
||||||
|
} as TFile;
|
||||||
|
const adapter = new ObsidianVaultAdapter(app);
|
||||||
|
|
||||||
|
const result = await adapter.read(file);
|
||||||
|
|
||||||
|
expect(new Blob([result]).size).toBe(file.stat.size);
|
||||||
|
expect(result.charCodeAt(0)).toBe(0xfeff);
|
||||||
|
expect(adapterRead).toHaveBeenCalledWith(path);
|
||||||
|
expect(read).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user