Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "marktext",
"version": "1.2.0",
"version": "1.3.0",
"description": "MarkText",
"main": "./out/main/index.cjs",
"type": "module",
Expand Down
47 changes: 39 additions & 8 deletions src/main/filesystem/encoding.js
Original file line number Diff line number Diff line change
@@ -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',
Expand Down Expand Up @@ -37,9 +59,9 @@ const checkSequence = (buffer, sequence) => {
*
* @param {Buffer} buffer
* @param {boolean} autoGuessEncoding
* @returns {Encoding}
* @returns {Promise<Encoding>}
*/
export const guessEncoding = (buffer, autoGuessEncoding) => {
export const guessEncoding = async (buffer, autoGuessEncoding) => {
let isBom = false
let encoding = 'utf8'

Expand All @@ -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 }
Expand Down
2 changes: 1 addition & 1 deletion src/main/filesystem/markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.`)
Expand Down
Loading