mirror of
https://github.com/fengxxc/wechatmp2markdown.git
synced 2026-03-04 03:30:48 +08:00
fix: windows下, 文件名包含非法字符时, 用相似的Unicode字符进行替换; 长度超过255个字符时,保留前255个字符
This commit is contained in:
@@ -3,10 +3,10 @@ package format
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -30,6 +30,33 @@ func Format(article parse.Article) (string, map[string][]byte) {
|
||||
return result, saveImageBytes
|
||||
}
|
||||
|
||||
// windows下, 文件名包含非法字符时, 用相似的Unicode字符进行替换; 长度超过255个字符时,保留前255个字符
|
||||
func legalizationFilenameForWindows(name string) string {
|
||||
// Windows文件名不能包含这些字符
|
||||
invalidChars := regexp.MustCompile(`[\\/:*?\"<>|]`)
|
||||
|
||||
// 如果包含非法字符,则替换
|
||||
if invalidChars.MatchString(name) {
|
||||
name = strings.ReplaceAll(name, "<", "≺")
|
||||
name = strings.ReplaceAll(name, ">", "≻")
|
||||
name = strings.ReplaceAll(name, ":", ":")
|
||||
name = strings.ReplaceAll(name, "\"", "“")
|
||||
name = strings.ReplaceAll(name, "/", "∕")
|
||||
name = strings.ReplaceAll(name, "\\", "∖")
|
||||
name = strings.ReplaceAll(name, "|", "∣")
|
||||
name = strings.ReplaceAll(name, "?", "?")
|
||||
name = strings.ReplaceAll(name, "*", "⁎")
|
||||
}
|
||||
|
||||
// 文件名最大长度255字符
|
||||
if len(name) > 255 {
|
||||
// 超出就截断文件名
|
||||
name = name[:255]
|
||||
}
|
||||
|
||||
return name
|
||||
}
|
||||
|
||||
// FormatAndSave fomat article and save to local file
|
||||
func FormatAndSave(article parse.Article, filePath string) error {
|
||||
// basrPath := filepath.Join(filePath, )
|
||||
@@ -55,6 +82,9 @@ func FormatAndSave(article parse.Article, filePath string) error {
|
||||
fileName = filePath
|
||||
} else {
|
||||
title := strings.TrimSpace(article.Title.Val.(string))
|
||||
if isWin {
|
||||
title = legalizationFilenameForWindows(title)
|
||||
}
|
||||
// title := "thisistitle"
|
||||
basePath = filepath.Join(filePath, title)
|
||||
fileName = filepath.Join(basePath, title+".md")
|
||||
@@ -89,7 +119,7 @@ func FormatAndSave(article parse.Article, filePath string) error {
|
||||
f.Write(buf.Bytes())
|
||||
}
|
||||
}
|
||||
return ioutil.WriteFile(fileName, []byte(result), 0644)
|
||||
return os.WriteFile(fileName, []byte(result), 0644)
|
||||
}
|
||||
|
||||
func formatTitle(piece parse.Piece) string {
|
||||
|
||||
Reference in New Issue
Block a user