Skip to content

Commit

Permalink
feat: ✨ add markdown diff utility and integrate in UI
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan committed Jan 7, 2025
1 parent 4ff9381 commit 90ec0c6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
21 changes: 21 additions & 0 deletions packages/core/src/mddiff.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { diffLines } from "diff"
import { fenceMD } from "./mkmd"

export function markdownDiff(
oldStr: string,
newStr: string,
options?: {
lang?: string
ignoreWhitespace?: boolean
}
) {
const { lang, ...rest } = options || {}

if (!oldStr) return fenceMD(newStr, lang)

const changes = diffLines(oldStr || "", newStr || "", rest)
const source = changes
.map((c) => `${c.added ? "+" : c.removed ? "-" : " "}${c.value}`)
.join("")
return fenceMD(source, "diff")
}
13 changes: 2 additions & 11 deletions packages/web/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import { lookupMime } from "../../core/src/mime"
import dedent from "dedent"
import { convertAnnotationsToMarkdown } from "../../core/src/annotations"
import "remark-github-blockquote-alert/alert.css"
import { markdownDiff } from "../../core/src/mddiff"

const urlParams = new URLSearchParams(window.location.hash)
const apiKey = urlParams.get("api-key")
Expand Down Expand Up @@ -814,17 +815,7 @@ function FilesTabPanel() {
?.map(
([filename, content], i) =>
dedent`### ${filename}
\`\`\`\`\`
${content.after}
\`\`\`\`\`
${
content.before
? dedent`- original
\`\`\`\`\`
${content.before}
\`\`\`\`\``
: ""
}
${markdownDiff(content.before, content.after, { lang: "txt" })}
${content.validation?.pathValid ? `- output path validated` : ""}
${
content.validation?.schema
Expand Down

0 comments on commit 90ec0c6

Please sign in to comment.