Skip to content

Conversation

@dazhu6666
Copy link

@dazhu6666 dazhu6666 commented Feb 10, 2026

1.新增扩展
模板
image

效果
image

2.修复bug
修复 当渲染table时使用 Descendants 会自动提取所有的tableRow,包括嵌套的tableRow,改用Elements,只提取子级的tr。
不修改效果是出现大量的无效tr

@gemini-code-assist
Copy link

Summary of Changes

Hello @dazhu6666, 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!

此拉取请求扩展了 MiniWord 库的表格渲染功能,使其能够根据模板标签的分布智能地选择列表数据的渲染方向。现在,如果表格中只有一个单元格包含列表数据模板,数据将横向填充到单元格中;否则,将沿用原有的纵向行渲染方式。这为用户提供了更大的灵活性,以适应不同的表格布局需求。

Highlights

  • 表格渲染模式扩展: 引入了新的逻辑来判断表格中的列表数据是应该进行横向渲染还是纵向渲染。
  • 横向单元格渲染: 当表格中只有一个单元格包含列表数据模板时,列表的每个项将填充到同一行中的不同单元格,实现数据的横向扩展。
  • 纵向行渲染保持: 当表格中有多个单元格包含列表数据模板时,将保持原有的纵向渲染行为,即每个列表项生成一个新行。
  • 模板匹配优化: 定义了新的正则表达式 regexStr 用于更精确地匹配 {{Items.PropName}} 格式的列表数据模板。

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • src/MiniWord/MiniWord.Implment.cs
    • 新增了 regexStr 变量用于匹配列表数据模板。
    • 引入了 isHorizontal 变量,根据表格中包含列表数据模板的单元格数量来决定渲染方向。
    • 修改了 GenerateTable 方法,根据 isHorizontal 的值实现横向或纵向的列表数据渲染逻辑。
    • 在横向渲染模式下,列表项被收集并填充到克隆的模板行单元格中,然后添加到表格。
    • 在纵向渲染模式下,保持了原有的列表项生成新行的逻辑。
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request extends table rendering functionality, adding support for cell-by-cell rendering and enabling horizontal rendering mode when a template has a single placeholder. However, it introduces potential security vulnerabilities in the MiniWord library, including a high-severity arbitrary file read vulnerability due to unsanitized file paths in MiniWordPicture objects, which could lead to information disclosure. Additionally, a medium-severity Regular Expression Denial of Service (ReDoS) vulnerability exists because user-controlled input is used directly in regex construction without proper escaping. Indirect insecure deserialization is also noted as a potential risk. Beyond security concerns, the current implementation has several issues: there's room for performance optimization when detecting rendering mode, the horizontal rendering logic appears to only work for nested tables and cannot handle plain text, and data loss occurs at the end when the total number of data items is not a multiple of the number of columns. Specific code suggestions have been provided for performance and data loss, but deeper refactoring is needed to generalize the content capture mechanism for the nested table issue.

Comment on lines +169 to +171
var newTable = newTr.Descendants<Table>().FirstOrDefault();
if (newTable != null)
elementList.Add(newTable.CloneNode(true));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

当前的水平渲染逻辑似乎只支持当占位符渲染为嵌套表格(Table)时的情况。如果占位符被替换为纯文本或其他类型的元素,newTr.Descendants<Table>().FirstOrDefault() 将返回 null,导致 elementList 为空,水平渲染逻辑不会添加任何内容。这使得该功能在处理非表格内容时会失效,是一个比较严重的缺陷。需要将此逻辑通用化,以捕获被渲染单元格内的所有类型内容。

Comment on lines 203 to 205
}

tr.Remove();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

在水平渲染模式下(isHorizontaltrue),如果数据列表中的项目总数不是表格列数的整数倍,循环结束后elementList中剩余的项目将会被丢弃,不会被渲染到文档中,这会导致数据丢失。您需要在循环之后添加逻辑来处理这些剩余的项目,为它们生成最后一行。

                        }

                        if (isHorizontal && elementList.Any())
                        {
                            var templateTr = tr.CloneNode(true);
                            var cells = templateTr.Elements<TableCell>().ToList();
                            for (int i = 0; i < cells.Count; i++)
                            {
                                var tCell = cells[i];
                                tCell.RemoveAllChildren();
                                if (i < elementList.Count)
                                {
                                    tCell.Append(elementList[i]);
                                }
                            }
                            table.Append(templateTr);
                        }

                        tr.Remove();

var trs = table.Descendants<TableRow>().ToArray(); // remember toarray or system will loop OOM;
var regexStr = "(?<={{).*?\\..*?(?=}})";
//计算是否只有一个cell存在指令,如果超过1个则纵向渲染,否则横向渲染。
bool isHorizontal = table.Elements<TableRow>().SelectMany(s => s.Elements<TableCell>()).Count(w => Regex.Matches(w.InnerText, regexStr).Count > 0) <= 1;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

为了提高性能,建议优化此处的LINQ查询。当前的Count()会遍历所有单元格,即使在找到多个匹配项后也是如此。使用Where(...).Take(2).Count()可以在找到两个匹配的单元格后提前终止,从而提高效率。

另外,正则表达式regexStr在循环中被多次使用。为了获得更好的性能,建议将其定义为static readonly Regex字段并使用RegexOptions.Compiled进行编译。

            bool isHorizontal = table.Elements<TableRow>().SelectMany(s => s.Elements<TableCell>()).Where(w => Regex.IsMatch(w.InnerText, regexStr)).Take(2).Count() <= 1;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant