From 7a78dee13f643e5f28189470a6de8fdae4e95925 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 26 Jan 2026 04:27:48 +0000 Subject: [PATCH 1/6] Initial plan From 8ce68706e95470a3cfa539ffc8dd23a05564b563 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 26 Jan 2026 04:31:18 +0000 Subject: [PATCH 2/6] Make ced module optional to prevent Windows startup crash Co-authored-by: peterjthomson <9357736+peterjthomson@users.noreply.github.com> --- src/main/filesystem/encoding.js | 47 +++++++++++++++++++++++++++------ src/main/filesystem/markdown.js | 2 +- 2 files changed, 40 insertions(+), 9 deletions(-) diff --git a/src/main/filesystem/encoding.js b/src/main/filesystem/encoding.js index 8e41fd3d..5f94baf4 100644 --- a/src/main/filesystem/encoding.js +++ b/src/main/filesystem/encoding.js @@ -1,4 +1,26 @@ -import ced from 'ced' +// Lazy-loaded ced module for character encoding detection. +// This is optional - if it fails to load (e.g., due to native module issues on Windows), +// we'll fall back to UTF-8 encoding. +let ced = null +let cedLoadAttempted = false + +async function getCed() { + if (cedLoadAttempted) { + return ced + } + + cedLoadAttempted = true + try { + const cedModule = await import('ced') + ced = cedModule.default + } catch (error) { + console.warn('Failed to load ced module for encoding detection:', error.message) + console.warn('Will default to UTF-8 encoding when autoGuessEncoding is enabled') + ced = null + } + + return ced +} const CED_ICONV_ENCODINGS = { 'BIG5-CP950': 'big5', @@ -37,9 +59,9 @@ const checkSequence = (buffer, sequence) => { * * @param {Buffer} buffer * @param {boolean} autoGuessEncoding - * @returns {Encoding} + * @returns {Promise} */ -export const guessEncoding = (buffer, autoGuessEncoding) => { +export const guessEncoding = async (buffer, autoGuessEncoding) => { let isBom = false let encoding = 'utf8' @@ -65,11 +87,20 @@ export const guessEncoding = (buffer, autoGuessEncoding) => { // Auto guess encoding, otherwise use UTF8. if (autoGuessEncoding) { - encoding = ced(buffer) - if (CED_ICONV_ENCODINGS[encoding]) { - encoding = CED_ICONV_ENCODINGS[encoding] - } else { - encoding = encoding.toLowerCase().replace(/-_/g, '') + const cedModule = await getCed() + if (cedModule) { + try { + encoding = cedModule(buffer) + if (CED_ICONV_ENCODINGS[encoding]) { + encoding = CED_ICONV_ENCODINGS[encoding] + } else { + encoding = encoding.toLowerCase().replace(/-_/g, '') + } + } catch (error) { + console.warn('Failed to detect encoding using ced:', error.message) + // Fall back to UTF-8 + encoding = 'utf8' + } } } return { encoding, isBom } diff --git a/src/main/filesystem/markdown.js b/src/main/filesystem/markdown.js index 38420578..09cc3b20 100644 --- a/src/main/filesystem/markdown.js +++ b/src/main/filesystem/markdown.js @@ -87,7 +87,7 @@ export const loadMarkdownFile = async ( let buffer = await fsPromises.readFile(path.resolve(pathname)) - const encoding = guessEncoding(buffer, autoGuessEncoding) + const encoding = await guessEncoding(buffer, autoGuessEncoding) const supported = iconv.encodingExists(encoding.encoding) if (!supported) { throw new Error(`"${encoding.encoding}" encoding is not supported.`) From 7ea0f51b9122e5cd0c3ca60c0306b77e6be5153b Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 26 Jan 2026 04:31:52 +0000 Subject: [PATCH 3/6] Fix regex pattern for encoding normalization Co-authored-by: peterjthomson <9357736+peterjthomson@users.noreply.github.com> --- src/main/filesystem/encoding.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/filesystem/encoding.js b/src/main/filesystem/encoding.js index 5f94baf4..f5b3be43 100644 --- a/src/main/filesystem/encoding.js +++ b/src/main/filesystem/encoding.js @@ -94,7 +94,7 @@ export const guessEncoding = async (buffer, autoGuessEncoding) => { if (CED_ICONV_ENCODINGS[encoding]) { encoding = CED_ICONV_ENCODINGS[encoding] } else { - encoding = encoding.toLowerCase().replace(/-_/g, '') + encoding = encoding.toLowerCase().replace(/[-_]/g, '') } } catch (error) { console.warn('Failed to detect encoding using ced:', error.message) From e32c5300a333e09660514fa13133f930abb11192 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 26 Jan 2026 04:32:52 +0000 Subject: [PATCH 4/6] Revert "Fix regex pattern for encoding normalization" This reverts commit 7ea0f51b9122e5cd0c3ca60c0306b77e6be5153b. --- src/main/filesystem/encoding.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/filesystem/encoding.js b/src/main/filesystem/encoding.js index f5b3be43..5f94baf4 100644 --- a/src/main/filesystem/encoding.js +++ b/src/main/filesystem/encoding.js @@ -94,7 +94,7 @@ export const guessEncoding = async (buffer, autoGuessEncoding) => { if (CED_ICONV_ENCODINGS[encoding]) { encoding = CED_ICONV_ENCODINGS[encoding] } else { - encoding = encoding.toLowerCase().replace(/[-_]/g, '') + encoding = encoding.toLowerCase().replace(/-_/g, '') } } catch (error) { console.warn('Failed to detect encoding using ced:', error.message) From 20282de6722a8771f750a3a5db7903325f864ecd Mon Sep 17 00:00:00 2001 From: Peter Thomson Date: Mon, 26 Jan 2026 18:39:01 +1300 Subject: [PATCH 5/6] Fix trailing whitespace in encoding.js Remove trailing spaces on blank lines to pass ESLint. --- src/main/filesystem/encoding.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/filesystem/encoding.js b/src/main/filesystem/encoding.js index 5f94baf4..511a091d 100644 --- a/src/main/filesystem/encoding.js +++ b/src/main/filesystem/encoding.js @@ -8,7 +8,7 @@ async function getCed() { if (cedLoadAttempted) { return ced } - + cedLoadAttempted = true try { const cedModule = await import('ced') @@ -18,7 +18,7 @@ async function getCed() { console.warn('Will default to UTF-8 encoding when autoGuessEncoding is enabled') ced = null } - + return ced } From 8845af86e7817472a1d8ea96db09e676629bcbaf Mon Sep 17 00:00:00 2001 From: Peter Thomson Date: Mon, 26 Jan 2026 18:46:10 +1300 Subject: [PATCH 6/6] Bump version to 1.3.0 Includes Windows startup crash fix (ced module made optional) --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 0f2c312c..dc1afb7a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "marktext", - "version": "1.2.0", + "version": "1.3.0", "description": "MarkText", "main": "./out/main/index.cjs", "type": "module",