-
Notifications
You must be signed in to change notification settings - Fork 3
Sanitize search queries and ensure config file exists #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hanfoo
wants to merge
2
commits into
raywill:main
Choose a base branch
from
hanfoo:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -689,8 +689,9 @@ ipcMain.on("search-files", async (event, query) => { | |
| }); | ||
|
|
||
| function processContent(file, title, titleLineNum, lines, query, results) { | ||
| const escapedQuery = escapeRegExp(query); | ||
| // 使用 Unicode 支持的正则表达式 | ||
| const regex = new RegExp(`(${query})`, "giu"); // 'u' 使正则表达式支持 Unicode,'i' 使匹配不区分大小写,'g' 使匹配全局 | ||
| const regex = new RegExp(`(${escapedQuery})`, "giu"); // 'u' 使正则表达式支持 Unicode,'i' 使匹配不区分大小写,'g' 使匹配全局 | ||
| const matches = new Set(); // 使用 Set 来去重 | ||
|
|
||
| if (regex.test(title)) { | ||
|
|
@@ -723,6 +724,8 @@ function processContent(file, title, titleLineNum, lines, query, results) { | |
| } | ||
|
|
||
| function formatResults(results, query) { | ||
| const escapedQuery = escapeRegExp(query); | ||
|
|
||
| return results.map((result) => { | ||
| const { file, title, titleLineNum, matches } = result; | ||
|
|
||
|
|
@@ -736,7 +739,7 @@ function formatResults(results, query) { | |
| match.index + | ||
| ');return false;">' + | ||
| match.content.replace( | ||
| new RegExp(`(${query})`, "giu"), | ||
| new RegExp(`(${escapedQuery})`, "giu"), | ||
| `<b style='color:#ea4335'>$1</b>`, | ||
| ) + | ||
| "</a>" | ||
|
|
@@ -753,6 +756,10 @@ function formatResults(results, query) { | |
| }); | ||
| } | ||
|
|
||
| function escapeRegExp(text) { | ||
| return text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); | ||
| } | ||
|
|
||
| ipcMain.on("open-search-file", (event, fName, lineNo) => { | ||
| var filePath = path.join(dirName, fName); | ||
| shellOpenPath(filePath, lineNo); | ||
|
|
@@ -809,62 +816,73 @@ function createSettingWindow() { | |
| }); | ||
| } | ||
|
|
||
| function ensureConfigFile() { | ||
| const defaultConfig = { | ||
| labels: | ||
| "#todo weekly,#todo monthly,#note weekly,#note monthly,#meeting 7 days", | ||
| writer: "md", | ||
| template: "", | ||
| user_defined_file: "", | ||
| application: "", | ||
| }; | ||
|
|
||
| if (!fs.existsSync(configName)) { | ||
| fs.mkdirSync(path.dirname(configName), { recursive: true }); | ||
| fs.writeFileSync(configName, JSON.stringify(defaultConfig, null, 2)); | ||
| return defaultConfig; | ||
| } | ||
|
|
||
| try { | ||
| return JSON.parse(fs.readFileSync(configName)); | ||
| } catch { | ||
| fs.writeFileSync(configName, JSON.stringify(defaultConfig, null, 2)); | ||
| return defaultConfig; | ||
| } | ||
| } | ||
|
|
||
| var initMenu = function (appIcon) { | ||
| var labels = ""; | ||
| try { | ||
| const config = JSON.parse(fs.readFileSync(configName)); | ||
| if (config) { | ||
| labels = config.labels; | ||
| var needUpgrade = false; | ||
| if (config.user_defined_file) { | ||
| // new version | ||
| userDefinedFiles = config.user_defined_file.split(/[,;]/); | ||
| } else { | ||
| config.user_defined_file = ""; | ||
| userDefinedFiles = []; | ||
| needUpgrade = true; | ||
| } | ||
| if (config.writer) { | ||
| // new version | ||
| fileExtension = config.writer; | ||
| } else { | ||
| // upgrade older version | ||
| fileExtension = "md"; | ||
| config.writer = "md"; | ||
| needUpgrade = true; | ||
| } | ||
| if (config.template) { | ||
| newPageTemplate = config.template; | ||
| } else { | ||
| newPageTemplate = ""; | ||
| config.template = ""; | ||
| needUpgrade = true; | ||
| } | ||
| if (config.application) { | ||
| customizedEditorApplication = config.application; | ||
| } else { | ||
| customizedEditorApplication = ""; | ||
| config.application = ""; | ||
| needUpgrade = true; | ||
| } | ||
|
|
||
| const config = ensureConfigFile(); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks. This one is neat. I will accept it. |
||
| if (config) { | ||
| labels = config.labels; | ||
| var needUpgrade = false; | ||
| if (config.user_defined_file) { | ||
| // new version | ||
| userDefinedFiles = config.user_defined_file.split(/[,;]/); | ||
| } else { | ||
| config.user_defined_file = ""; | ||
| userDefinedFiles = []; | ||
| needUpgrade = true; | ||
| } | ||
| if (config.writer) { | ||
| // new version | ||
| fileExtension = config.writer; | ||
| } else { | ||
| // upgrade older version | ||
| fileExtension = "md"; | ||
| config.writer = "md"; | ||
| needUpgrade = true; | ||
| } | ||
| if (config.template) { | ||
| newPageTemplate = config.template; | ||
| } else { | ||
| newPageTemplate = ""; | ||
| config.template = ""; | ||
| needUpgrade = true; | ||
| } | ||
| if (config.application) { | ||
| customizedEditorApplication = config.application; | ||
| } else { | ||
| customizedEditorApplication = ""; | ||
| config.application = ""; | ||
| needUpgrade = true; | ||
| } | ||
| if (needUpgrade) { | ||
| fs.writeFileSync(configName, JSON.stringify(config, null, 2)); | ||
| } | ||
| } catch { | ||
| fs.exists(configName, (exists) => { | ||
| if (!exists) { | ||
| const data = {}; | ||
| labels = | ||
| "#todo weekly,#todo monthly,#note weekly,#note monthly,#meeting 7 days"; | ||
| data.labels = labels; | ||
| data.writer = "md"; | ||
| data.template = ""; | ||
| data.user_defined_file = ""; | ||
| data.application = ""; | ||
| fs.writeFileSync(configName, JSON.stringify(data, null, 2)); | ||
| } | ||
| }); | ||
| console.log("parse json file fail"); | ||
| } | ||
|
|
||
| var menuArr = []; | ||
|
|
||
| menuArr.push({ | ||
|
|
@@ -992,12 +1010,16 @@ app.on("ready", function () { | |
| appIcon.setToolTip("日志保存路径:" + dirName); | ||
| initMenu(appIcon); | ||
| appIcon.on("click", openDailyFile); | ||
| fs.watch(configName, (event, filename) => { | ||
| if (filename && event == "change") { | ||
| initMenu(appIcon); | ||
| console.log(`${filename}文件发生更新,更新菜单`); | ||
| } | ||
| }); | ||
|
|
||
| if (fs.existsSync(configName)) { | ||
| fs.watch(configName, (event, filename) => { | ||
| if (filename && event == "change") { | ||
| initMenu(appIcon); | ||
| console.log(`${filename}文件发生更新,更新菜单`); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| openDailyFile(); | ||
| }); | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is by design. With reg exp enabled, you can search your text using patterns such as "AI.*向量".
This is very powerful.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't want to escape any of the user input.