This commit is contained in:
Nan Yu committed 2021-02-21 11:52:00 -08:00
1 parent 3bace8c9e4
commit 3df82c500b
11 files changed
+208 -3

No files matched your search

+32
View File
@@ -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 <Container dangerouslySetInnerHTML={{ __html: diff }} />;
}
const Container = styled.div`
ins {
color: green;
text-decoration: none;
}
del {
color: red;
text-decoration: strikethrough;
}
`;
+9
View File
@@ -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() {
<Route exact path="/" component={Login} />
<Route exact path="/create" component={Login} />
<Route exact path="/share/:shareId" component={KeyedDocument} />
{/* TODO this is just a development harness -- delete later */}
<Route
exact
path="/revisionDiffExample"
component={RevisionDiffExample}
/>
<Authenticated>
<AuthenticatedRoutes />
</Authenticated>
+3 -2
View File
@@ -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"
}
}
+9
View File
@@ -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;
+3 -1
View File
@@ -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,
+28
View File
@@ -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
+29
View File
@@ -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
@@ -0,0 +1,23 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`should diff a new revision of a document 1`] = `
"<h1>Heading 1</h1>
<h2>Heading 2</h2>
<p>This is a test <del data-operation-index=\\"1\\">paragraph</del></p><p><ins data-operation-index=\\"1\\">paragraph. </ins>This <del data-operation-index=\\"3\\">is a second test paragraph. It will be deleted.</del><ins data-operation-index=\\"3\\">was added.</ins></p>
<ul>
<li>list item 1</li><li><del data-operation-index=\\"5\\">list item 2</del></li>
</ul>
<pre><code>this is a codeblock
</code></pre><p data-diff-node=\\"ins\\" data-operation-index=\\"7\\"><ins data-operation-index=\\"7\\">This is a new paragraph.</ins></p>
<div class=\\"notice\\">
<p>This is an info block</p>
</div>
<p><span>This is a placeholder</span></p>
<p><span>this is a highlight</span></p>
<ul>
<li checked=\\"true\\">checklist item 1</li>
<li checked=\\"true\\">checklist item 2</li>
<li>checklist item 3</li>
<li data-diff-node=\\"ins\\" data-operation-index=\\"9\\"><ins data-operation-index=\\"9\\">checklist item 4</ins></li><li checked=\\"true\\" data-diff-node=\\"ins\\" data-operation-index=\\"9\\"><ins data-operation-index=\\"9\\">checklist item 5</ins></li></ul>
"
`;
+39
View File
@@ -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);
}
+28
View File
@@ -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", () => {});
+5
View File
@@ -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"