Repository: marp-team/marp-vscode (ts)

Issue:
The exported result is incorrect if the document had not UTF-8 encoding
### Version of Marp Tool

Marp for VS Code v3.1.0

### Operating System

macOS

### Environment

OS version: macOS Sequoia 15.3.1
VS Code version (Marp for VS Code): VS Code 1.97.2

### How to reproduce

1. Create a new Markdown file just like this:

```
🐣
```

2. Save Markdown file with UTF-16 LE encoding
3. Export slide deck to any format.

### Expected behavior

Show a slide page with the content `🐣`

### Actual behavior

A slide page has incorrect encoding.

### Additional information

Originally reported in marp-team/marp#558.

Extension could not deal with that due to lack of VS Code API for getting a encoding for the current document. But now the latest VS Code 1.98 has a newly proposed API for getting encoding.
https://code.visualstudio.com/updates/v1_98#_text-encodings
https://github.com/microsoft/vscode/blob/501ee833b16b8e83ba656c46e0888aadd9d2db04/src/vscode-dts/vscode.proposed.textDocumentEncoding.d.ts#L1

To fix that, Marp for VS Code should use an temporary file as an input if detected non UTF-8 encoding.
https://github.com/marp-team/marp-vscode/blob/bc09bd5241c28889ec077a7820992599a0e307cd/src/marp-cli.ts#L30

Pull request:
Fix the encoding in the export output when Markdown file has been saved with non UTF-8 encoding
> Continue from #501

Fix #497 (marp-team/marp#558).

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index c79eb91..438a10e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -9,6 +9,10 @@
 - Upgrade Marp CLI to [v4.2.1](https://github.com/marp-team/marp-cli/releases/tag/v4.2.1) ([#515](https://github.com/marp-team/marp-vscode/pull/515))
 - Upgrade development Node.js and dependent packages ([#515](https://github.com/marp-team/marp-vscode/pull/515))
 
+### Fixed
+
+- Fixed incorrect export result when exporting Markdown with non UTF-8 encoding (Required VS Code 1.100 or later: [#497](https://github.com/marp-team/marp-vscode/issues/497), [#510](https://github.com/marp-team/marp-vscode/pull/510))
+
 ## v3.2.0 - 2025-04-06
 
 ### Breaking
diff --git a/src/marp-cli.ts b/src/marp-cli.ts
index a0724c0..cd390cb 100644
--- a/src/marp-cli.ts
+++ b/src/marp-cli.ts
@@ -26,8 +26,16 @@ export interface MarpCLIErrorHandler {
 }
 
 export async function createWorkFile(doc: TextDocument): Promise<WorkFile> {
+  // @ts-expect-error `encoding` is available in VS Code 1.100+ (remove this comment when updated the type definition of VS Code)
+  const { encoding } = doc
+  const encodingAPIavailable = !!encoding
+
   // Use a real file if posibble
-  if (doc.uri.scheme === 'file' && !doc.isDirty) {
+  if (
+    doc.uri.scheme === 'file' &&
+    !doc.isDirty &&
+    (!encodingAPIavailable || encoding === 'utf8' || encoding === 'utf8bom')
+  ) {
     return { path: doc.uri.fsPath, cleanup: () => Promise.resolve() }
   }
 


Test patch:
diff --git a/src/marp-cli.test.ts b/src/marp-cli.test.ts
index 0e710f4..ff95961 100644
--- a/src/marp-cli.test.ts
+++ b/src/marp-cli.test.ts
@@ -86,6 +86,30 @@ describe('#createWorkFile', () => {
     )
   })
 
+  it('creates tmpfile when passed clean file but non UTF-8 encoding', async () => {
+    const workFile = await createWorkFile({
+      getText: jest.fn(() => 'example'),
+      isDirty: false,
+      uri: { scheme: 'file', fsPath: '/tmp/clean.md' },
+      encoding: 'utf16le',
+    } as any)
+
+    expect(
+      workFile.path.startsWith(path.join('/tmp', '.marp-vscode-tmp')),
+    ).toBe(true)
+
+    expect(workspace.fs.writeFile).toHaveBeenCalledWith(
+      expect.objectContaining({ fsPath: workFile.path }),
+      textEncoder.encode('example'),
+    )
+
+    await workFile.cleanup()
+    expect(workspace.fs.delete).toHaveBeenCalledWith(
+      expect.objectContaining({ fsPath: workFile.path }),
+      expect.any(Object),
+    )
+  })
+
   it('creates tmpfile to workspace root when failed creating to same dir', async () => {
     // Simulate that creation to same directory is not permitted
     const writeFileMock = jest