Logo

Logo

diff.js+diff2html-ui.js 使用实例

Pastore Antonio
Pastore Antonio 2025年05月04日
857 阅读 0 评论 约 3714 字 阅读约 8 分钟
<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>文件差异对比</title>
    <link rel="stylesheet" href="https://unpkg.com/diff2html/bundles/css/diff2html.min.css">
    <style>
        body {
            font-family: Arial, sans-serif;
        }
        #result {
            border: 1px solid #ddd;
            padding: 10px;
            margin-top: 20px;
            overflow: auto;
        }
    </style>
</head>
<body>
<h1>对比远程文件与本地文本差异</h1>
<div>
    <textarea id="localText" rows="10" cols="50" placeholder="输入本地文本..."></textarea>
</div>
<button id="compareBtn">比较</button>
<div id="result"></div>

<script src="https://cdn.jsdelivr.net/npm/diff@5.0.0/dist/diff.min.js"></script>
<script src="
https://cdn.jsdelivr.net/npm/diff2html@3.4.48/bundles/js/diff2html-ui.min.js
"></script>
<script>
    document.getElementById('compareBtn').addEventListener('click', function() {
        const remoteFileUrl = 'http://idipportal-dev.ied.com/data/8ball_IDIP_20240821_238.xml'; // 替换为目标远程文件的 URL
        const localText = document.getElementById('localText').value;

        // 使用 Fetch API 获取远程文件内容
        fetch(remoteFileUrl)
            .then(response => {
                if (!response.ok) {
                    throw new Error('网络错误');
                }
                return response.text();
            })
            .then(remoteText => {
                // 使用 Diff.js 计算差异
                const diff = Diff.diffLines(remoteText,localText);

                // 将 diff 转换为 Diff2Html 所需的格式
                let addedCount = 0;
                let removedCount = 0;
                const ttdiffString = diff.map(part => {
                    const prefix = part.added ? '+' : part.removed ? '-' : ' ';
                    // 更新计数器
                    if (part.added) {
                        addedCount += part.count || part.value.split('\n').length; // 计算添加的行数
                    }
                    if (part.removed) {
                        removedCount += part.count || part.value.split('\n').length; // 计算删除的行数
                    }
                    return part.value.split('\n').map(line => `${prefix}${line}`).join('\n');
                }).join('\n');

                const diffString = `diff --git a/8ball_IDIP_20240821_238.xml b/8ball_IDIP_20240821_238.xml
index 0000001..0ddf2ba
--- a/8ball_IDIP_20240821_238.xml
+++ b/8ball_IDIP_20240821_238.xml
@@ -${removedCount} +${addedCount} @@
${ttdiffString}
`;

                var targetElement = document.getElementById('result');
                var configuration = {
                    drawFileList: false,
                    fileListToggle: false,
                    fileListStartVisible: false,
                    fileContentToggle: false,
                    matching: 'lines',
                    outputFormat: 'side-by-side',
                    synchronisedScroll: true,
                    highlight: true,
                    renderNothingWhenEmpty: false,
                };
                var diff2htmlUi = new Diff2HtmlUI(targetElement, diffString, configuration);
                diff2htmlUi.draw();
                diff2htmlUi.highlightCode();
            })
            .catch(error => {
                console.error('获取远程文件失败:', error);
                document.getElementById('result').innerText = '错误: ' + error.message;
            });
    });
</script>
</body>
</html>

参考文档:

https://github.com/rtfpessoa/diff2html-cli

https://github.com/kpdecker/jsdiff#change-objects

https://www.cnblogs.com/kaikaiya/p/18374182

查看完整代码

橙子主题打折出售

其实我不卖,主要是这里是放广告的,所以就放了一个
毕竟主题都没做完,卖了也是坑.

购买它
部分文章可能存在转载,如果涉及到侵权,请联系删除文章。

留言板

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

5 - 4 = ?

探索AIGC相关的精彩内容,共 15 篇文章

Azure AI 服务之语音识别

简介 Azure AI 服务中的语音识别 API 是微软提供的一项先进技术,旨在帮助开发者轻松实现语 ... diff.js+diff2html-ui.js 使用实例

2026-02-17 · Xzavier Aaron
MCP | 一文详解什么是 MCP以及 MCP 可以做什么

一、什么是 MCP MCP(Model Context Protocol)是一个专为大型语言模型(L ... diff.js+diff2html-ui.js 使用实例

2026-02-14 · Shen, Luke
你的工作流程,值得一个“全自动数字分身”:录制、截图、成文,一气呵成

一、一句话认识 TestFlow Recorder 在数字化工作环境中,如何准确记录操作步骤并生成清 ... diff.js+diff2html-ui.js 使用实例

2026-02-14 · Xzavier Aaron
Flowise 前端框架配置指南

用户需求 问题:有没有适合配置 Flowise 的前端框架? 目标:寻找类似 Open WebUI ... diff.js+diff2html-ui.js 使用实例

2026-02-14 · Xzavier Aaron