Merge pull request #314 from esteemapp/editor-patch

Fix permlink and create patching function
This commit is contained in:
uğur erdal 2019-01-02 12:32:51 +03:00 committed by GitHub
commit 8feea5ef66
5 changed files with 43 additions and 10 deletions

View File

@ -32,6 +32,7 @@
"buffer": "^5.2.1",
"core-js": "^2.6.0",
"crypto-js": "^3.1.9-1",
"diff-match-patch": "^1.0.4",
"dsteem": "^0.10.1",
"intl": "^1.2.5",
"moment": "^2.22.2",
@ -62,6 +63,7 @@
"redux-thunk": "^2.3.0",
"remarkable": "^1.7.1",
"rn-placeholder": "^1.3.2",
"speakingurl": "^14.0.1",
"steemconnect": "^2.0.1"
},
"devDependencies": {

View File

@ -207,6 +207,7 @@ class EditorContainer extends Component {
const meta = extractMetadata(fields.body);
const jsonMeta = makeJsonMetadata(meta, fields.tags);
//TODO: check if permlink is available github: #314 https://github.com/esteemapp/esteem-mobile/pull/314
const permlink = generatePermlink(fields.title);
const author = currentAccount.name;
const options = makeOptions(author, permlink);

View File

@ -1,17 +1,29 @@
import getSlug from 'speakingurl';
import { diff_match_patch } from 'diff-match-patch';
export const getWordsCount = text => (text && typeof text === 'string' ? text.replace(/^\s+|\s+$/g, '').split(/\s+/).length : 0);
export const generatePermlink = (text) => {
if (text) {
const re = /[^a-z0-9]+/gi;
const re2 = /^-*|-*$/g;
let permlink = text.replace(re, '-');
export const generatePermlink = (title, random = false) => {
if (title) {
const slug = getSlug(title);
let perm = slug.toString();
permlink = `${permlink.replace(re2, '').toLowerCase()}-id-${Math.random()
.toString(36)
.substr(2, 16)}`;
return permlink;
if (random) {
const rnd = (Math.random() + 1).toString(16).substring(2);
perm = `${slug.toString()}-${rnd}est`;
}
// STEEMIT_MAX_PERMLINK_LENGTH
if (perm.length > 255) {
perm = perm.substring(perm.length - 255, perm.length);
}
// only letters numbers and dashes
perm = perm.toLowerCase().replace(/[^a-z0-9-]+/g, '');
return perm;
}
return null;
};
export const generateReplyPermlink = (toAuthor) => {
@ -117,3 +129,11 @@ export const extractMetadata = (body) => {
return out;
};
export const createPatch = (text1, text2) => {
const dmp = new diff_match_patch();
if (!text1 && text1 === '') return undefined;
const patches = dmp.patch_make(text1, text2);
const patch = dmp.patch_toText(patches);
return patch;
};

View File

@ -2758,6 +2758,11 @@ detect-newline@^2.1.0:
resolved "https://registry.yarnpkg.com/detect-newline/-/detect-newline-2.1.0.tgz#f41f1c10be4b00e87b5f13da680759f2c5bfd3e2"
integrity sha1-9B8cEL5LAOh7XxPaaAdZ8sW/0+I=
diff-match-patch@^1.0.4:
version "1.0.4"
resolved "https://registry.yarnpkg.com/diff-match-patch/-/diff-match-patch-1.0.4.tgz#6ac4b55237463761c4daf0dc603eb869124744b1"
integrity sha512-Uv3SW8bmH9nAtHKaKSanOQmj2DnlH65fUpcrMdfdaOxUG02QQ4YGZ8AE7kKOMisF7UqvOlGKVYWRvezdncW9lg==
diff@^3.2.0:
version "3.5.0"
resolved "https://registry.yarnpkg.com/diff/-/diff-3.5.0.tgz#800c0dd1e0a8bfbc95835c202ad220fe317e5a12"
@ -7835,6 +7840,11 @@ spdx-license-ids@^3.0.0:
resolved "https://registry.yarnpkg.com/spdx-license-ids/-/spdx-license-ids-3.0.2.tgz#a59efc09784c2a5bada13cfeaf5c75dd214044d2"
integrity sha512-qky9CVt0lVIECkEsYbNILVnPvycuEBkXoMFLRWsREkomQLevYhtRKC+R91a5TOAQ3bCMjikRwhyaRqj1VYatYg==
speakingurl@^14.0.1:
version "14.0.1"
resolved "https://registry.yarnpkg.com/speakingurl/-/speakingurl-14.0.1.tgz#f37ec8ddc4ab98e9600c1c9ec324a8c48d772a53"
integrity sha512-1POYv7uv2gXoyGFpBCmpDVSNV74IfsWlDW216UPjbWufNf+bSU6GdbDsxdcxtfwb4xlI3yxzOTKClUosxARYrQ==
split-string@^3.0.1, split-string@^3.0.2:
version "3.1.0"
resolved "https://registry.yarnpkg.com/split-string/-/split-string-3.1.0.tgz#7cb09dda3a86585705c64b39a6466038682e8fe2"