✨ feat: 增加下载嵌入翻译和音译功能#612
Conversation
Summary of ChangesHello @Txt-Text, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! 此拉取请求引入了一项新功能,允许用户在下载歌曲时选择同时下载歌词的翻译和音译(罗马音)。它通过在设置界面添加新的配置选项,并重构了下载管理器中的歌词处理逻辑来实现,确保了多语言歌词能够被正确地解析和嵌入到下载文件中。 Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
| const findMatch = (map: Map<string, string>, currentTag: string) => { | ||
| // Exact match | ||
| const exact = map.get(currentTag); | ||
| if (exact) return exact; | ||
|
|
||
| // Fuzzy match | ||
| const tSec = timeStrToSeconds(currentTag.slice(1, -1)); | ||
| let bestTag: string | null = null; | ||
| let bestDiff = Infinity; | ||
| for (const key of Array.from(map.keys())) { | ||
| const kSec = timeStrToSeconds(key.slice(1, -1)); | ||
| const diff = Math.abs(kSec - tSec); | ||
| if (diff < bestDiff) { | ||
| bestDiff = diff; | ||
| bestTag = key; | ||
| } | ||
| } | ||
| if (bestTag && bestDiff < 0.5) { | ||
| return map.get(bestTag); | ||
| } | ||
| return null; | ||
| }; |
| // Append translation | ||
| if (tlyric) { | ||
| const transText = findMatch(tMap, timeTag); | ||
| if (transText) { | ||
| for (const lt of transText.split("\n")) { | ||
| if (lt.trim()) lines.push(`${timeTag}${lt}`); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Append romaji | ||
| if (romalrc) { | ||
| const romaText = findMatch(rMap, timeTag); | ||
| if (romaText) { | ||
| for (const lt of romaText.split("\n")) { | ||
| if (lt.trim()) lines.push(`${timeTag}${lt}`); | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
这部分附加翻译和音译歌词的逻辑几乎完全相同。为了提高代码的可维护性并减少重复,可以将其重构得更简洁。
注意:应用此建议前,需要先将 findMatch 函数的定义移出当前循环,正如我在另一条评论中提到的。
// Append translation and romaji
[
{ enabled: tlyric, map: tMap },
{ enabled: romalrc, map: rMap },
].forEach(({ enabled, map }) => {
if (enabled) {
const matchedText = findMatch(map, timeTag);
if (matchedText) {
for (const lt of matchedText.split("\n")) {
if (lt.trim()) lines.push(`${timeTag}${lt}`);
}
}
}
});
No description provided.