From 3df82c500bf98436f0af183dc786d4ee2349179d Mon Sep 17 00:00:00 2001 From: Nan Yu Date: Sun, 21 Feb 2021 11:52:00 -0800 Subject: [PATCH] wip --- app/components/RevisionDiffExample.js | 32 +++++++++++++++ app/routes/index.js | 9 +++++ package.json | 5 ++- server/api/revisions.js | 9 +++++ server/services/notifications.js | 4 +- server/test/fixtures/complex.md | 28 +++++++++++++ server/test/fixtures/complexModified.md | 29 ++++++++++++++ .../__snapshots__/revisionDiff.test.js.snap | 23 +++++++++++ server/utils/revisionDiff.js | 39 +++++++++++++++++++ server/utils/revisionDiff.test.js | 28 +++++++++++++ yarn.lock | 5 +++ 11 files changed, 208 insertions(+), 3 deletions(-) create mode 100644 app/components/RevisionDiffExample.js create mode 100644 server/test/fixtures/complex.md create mode 100644 server/test/fixtures/complexModified.md create mode 100644 server/utils/__snapshots__/revisionDiff.test.js.snap create mode 100644 server/utils/revisionDiff.js create mode 100644 server/utils/revisionDiff.test.js diff --git a/app/components/RevisionDiffExample.js b/app/components/RevisionDiffExample.js new file mode 100644 index 0000000000..ef2eae1b59 --- /dev/null +++ b/app/components/RevisionDiffExample.js @@ -0,0 +1,32 @@ +// @flow +import * as React from "react"; +import styled from "styled-components"; +import { client } from "utils/ApiClient"; + +export default function RevisionDiffExample() { + const [diff, setDiff] = React.useState(""); + + React.useEffect(() => { + const fetchData = async () => { + const res = await client.post("/revisions.diff_example", {}); + console.log({ res }); + setDiff(res.data.diff); + }; + + fetchData(); + }, []); + + return ; +} + +const Container = styled.div` + ins { + color: green; + text-decoration: none; + } + + del { + color: red; + text-decoration: strikethrough; + } +`; diff --git a/app/routes/index.js b/app/routes/index.js index 2be6870fb5..11ce0a651f 100644 --- a/app/routes/index.js +++ b/app/routes/index.js @@ -3,6 +3,7 @@ import * as React from "react"; import { Switch, Route } from "react-router-dom"; import DelayedMount from "components/DelayedMount"; import FullscreenLoading from "components/FullscreenLoading"; +import RevisionDiffExample from "components/RevisionDiffExample"; const Authenticated = React.lazy(() => import("components/Authenticated")); const AuthenticatedRoutes = React.lazy(() => import("./authenticated")); @@ -22,6 +23,14 @@ export default function Routes() { + + {/* TODO this is just a development harness -- delete later */} + + diff --git a/package.json b/package.json index 713983869c..b41c97ffd2 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "scripts": { "clean": "rimraf build", "build:i18n": "i18next 'app/**/*.js' 'server/**/*.js' && mkdir -p ./build/shared/i18n && cp -R ./shared/i18n/locales ./build/shared/i18n", - "build:server": "babel -d ./build/server ./server && babel -d ./build/shared ./shared && cp package.json ./build && ln -sf \"$(pwd)/webpack.config.dev.js\" ./build", + "build:server": "babel -d ./build/server ./server && babel -d ./build/shared ./shared && cp -R ./server/test/fixtures ./build/server/test/fixtures && cp package.json ./build && ln -sf \"$(pwd)/webpack.config.dev.js\" ./build", "build:webpack": "webpack --config webpack.config.prod.js", "build": "yarn clean && yarn build:webpack && yarn build:i18n && yarn build:server", "start": "node ./build/server/index.js", @@ -125,6 +125,7 @@ "mobx": "4.6.0", "mobx-react": "^6.2.5", "natural-sort": "^1.0.0", + "node-htmldiff": "^0.9.3", "nodemailer": "^4.4.0", "outline-icons": "^1.23.0", "oy-vey": "^0.10.0", @@ -210,4 +211,4 @@ "js-yaml": "^3.13.1" }, "version": "0.52.0" -} \ No newline at end of file +} diff --git a/server/api/revisions.js b/server/api/revisions.js index 4ec83708b2..f109f337b1 100644 --- a/server/api/revisions.js +++ b/server/api/revisions.js @@ -5,6 +5,7 @@ import auth from "../middlewares/authentication"; import { Document, Revision } from "../models"; import policy from "../policies"; import { presentRevision } from "../presenters"; +import { revisionDiffExample } from "../utils/revisionDiff"; import pagination from "./middlewares/pagination"; const { authorize } = policy; @@ -57,4 +58,12 @@ router.post("revisions.list", auth(), pagination(), async (ctx) => { }; }); +// TODO: dev harness for revision diffs +router.post("revisions.diff_example", auth(), pagination(), async (ctx) => { + const diff = await revisionDiffExample(); + ctx.body = { + data: { diff }, + }; +}); + export default router; diff --git a/server/services/notifications.js b/server/services/notifications.js index 9629ad40a5..b00f7b8079 100644 --- a/server/services/notifications.js +++ b/server/services/notifications.js @@ -22,7 +22,7 @@ export default class Notifications { return this.documentUpdated(event); case "collections.create": return this.collectionCreated(event); - default: + default: //TODO pick up revisions create! } } @@ -96,6 +96,8 @@ export default class Notifications { return; } + // TODO: calculate the diff in document revision and send it through to the mailer + mailer.documentNotification({ to: setting.user.email, eventName, diff --git a/server/test/fixtures/complex.md b/server/test/fixtures/complex.md new file mode 100644 index 0000000000..9cdc4db075 --- /dev/null +++ b/server/test/fixtures/complex.md @@ -0,0 +1,28 @@ +# Heading 1 + +## Heading 2 + +This is a test paragraph + +This is a second test paragraph. This is a second sentence. + +- list item 1 +- list item 2 + +``` +this is a codeblock +``` + +:::info +This is an info block +::: + +!!This is a placeholder!! + +==this is a highlight== + +- [ ] checklist item 1 +- [ ] checklist item 2 +- [x] checklist item 3 + + diff --git a/server/test/fixtures/complexModified.md b/server/test/fixtures/complexModified.md new file mode 100644 index 0000000000..0478017dc9 --- /dev/null +++ b/server/test/fixtures/complexModified.md @@ -0,0 +1,29 @@ +# Heading 1 + +## Heading 2 + +This is a test paragraph. This was added. + +- list item 1 + +``` +this is a codeblock +``` + +This is a new paragraph. + +:::info +This is an info block +::: + +!!This is a placeholder!! + +==this is a highlight== + +- [x] checklist item 1 +- [x] checklist item 2 +- [ ] checklist item 3 +- [ ] checklist item 4 +- [x] checklist item 5 + + diff --git a/server/utils/__snapshots__/revisionDiff.test.js.snap b/server/utils/__snapshots__/revisionDiff.test.js.snap new file mode 100644 index 0000000000..cad37a9f3a --- /dev/null +++ b/server/utils/__snapshots__/revisionDiff.test.js.snap @@ -0,0 +1,23 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`should diff a new revision of a document 1`] = ` +"

Heading 1

+

Heading 2

+

This is a test paragraph

paragraph. This is a second test paragraph. It will be deleted.was added.

+
    +
  • list item 1
  • list item 2
  • +
+
this is a codeblock
+

This is a new paragraph.

+
+

This is an info block

+
+

This is a placeholder

+

this is a highlight

+
    +
  • checklist item 1
  • +
  • checklist item 2
  • +
  • checklist item 3
  • +
  • checklist item 4
  • checklist item 5
+" +`; diff --git a/server/utils/revisionDiff.js b/server/utils/revisionDiff.js new file mode 100644 index 0000000000..d1b3f39c8c --- /dev/null +++ b/server/utils/revisionDiff.js @@ -0,0 +1,39 @@ +// @flow +import fs from "fs"; +import path from "path"; +import diff from "node-htmldiff"; +import markdownRules from "rich-markdown-editor/dist/lib/markdown/rules"; + +// markdown-it instance +const md = markdownRules({}); + +export default function revisionDiff(before: string, after: string) { + // The basic idea here is to first render the markdown and then diff the HTML + // - both sides will have valid HTML so we should have a valid diff as well + + const beforeHtml = md.render(before); + const afterHtml = md.render(after); + + console.log({ beforeHtml, afterHtml }); + + // DIFF! + + const diffHtml = diff(beforeHtml, afterHtml); + console.log("DIFF==>", diffHtml); + + return diffHtml; +} + +export async function revisionDiffExample() { + let before = await fs.promises.readFile( + path.resolve(__dirname, "..", "test", "fixtures", "complex.md"), + "utf8" + ); + + let after = await fs.promises.readFile( + path.resolve(__dirname, "..", "test", "fixtures", "complexModified.md"), + "utf8" + ); + + return revisionDiff(before, after); +} diff --git a/server/utils/revisionDiff.test.js b/server/utils/revisionDiff.test.js new file mode 100644 index 0000000000..f68068b04f --- /dev/null +++ b/server/utils/revisionDiff.test.js @@ -0,0 +1,28 @@ +// @flow +import fs from "fs"; +import path from "path"; +import revisionDiff from "./revisionDiff"; + +it("should diff a new revision of a document", async () => { + let fileValue1 = await fs.promises.readFile( + path.resolve(__dirname, "..", "test", "fixtures", "complex.md"), + "utf8" + ); + + let fileValue2 = await fs.promises.readFile( + path.resolve(__dirname, "..", "test", "fixtures", "complexModified.md"), + "utf8" + ); + + // markdown => HTML + // markdownIT inside of rich md editor + // expose API in lib, use direct path for now + // !! == checklist item all need classes to distinguish + + const diff = revisionDiff(fileValue1, fileValue2); + expect(diff).toMatchSnapshot(); + + // TODO hand evaluate the veracity fo the snapshot +}); + +it("should return empty string when the new revision has the same content", () => {}); diff --git a/yarn.lock b/yarn.lock index aa15eec21d..9b108b89d8 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8512,6 +8512,11 @@ node-forge@^0.9.0: resolved "https://registry.yarnpkg.com/node-forge/-/node-forge-0.9.2.tgz#b35a44c28889b2ea55cabf8c79e3563f9676190a" integrity sha512-naKSScof4Wn+aoHU6HBsifh92Zeicm1GDQKd1vp3Y/kOi8ub0DozCa9KpvYNCXslFHYRmLNiqRopGdTGwNLpNw== +node-htmldiff@^0.9.3: + version "0.9.3" + resolved "https://registry.yarnpkg.com/node-htmldiff/-/node-htmldiff-0.9.3.tgz#020704e381597e5e449a4708996edf23eebb7fcc" + integrity sha1-AgcE44FZfl5EmkcImW7fI+67f8w= + node-int64@^0.4.0: version "0.4.0" resolved "https://registry.yarnpkg.com/node-int64/-/node-int64-0.4.0.tgz#87a9065cdb355d3182d8f94ce11188b825c68a3b"