Last active
June 8, 2017 18:49
-
-
Save gabrielrtakeda/da9213eb1b1068090afd10cdb9e3b486 to your computer and use it in GitHub Desktop.
[Nossas] Slate Editor: purge empty link objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// requirements: | |
// - node.js version: >= v7.10.0 | |
// | |
// installation: | |
// - `mkdir slate-editor-purge-empty-links` (make a new directory) | |
// - `cd slate-editor-purge-empty-links` (enter into it) | |
// - `yarn init` (initialize yarn) | |
// - `yarn add lodash json-stringify-pretty-compact escape-json-node --save` (install dependencies) | |
// - download the script using the command below: | |
// - curl -s https://gist.githubusercontent.com/gabrielrtakeda/da9213eb1b1068090afd10cdb9e3b486/raw/129e4eaf4e2e3f869b64fa24732b5394f4384dda/slate-editor-purge-empty-links.js > ./migration.js | |
// - update the `updateSQL` function with your database table data | |
// - `node ./migration.js > migration.sql` (execute the migration) | |
// - take the **migration.sql** file and execute it on your database | |
// | |
var _ = require('lodash') | |
var stringify = require('json-stringify-pretty-compact') | |
var escapeJSON = require('escape-json-node') | |
const deep = (obj, key, value) => { | |
var keys = [] | |
for(var _key in obj) { | |
if (_key === key && (value ? obj[_key] === value : true)) { | |
keys.push(_key) | |
} | |
if(typeof obj[_key] === 'object') { | |
var subkeys = deep(obj[_key], key, value) | |
keys = keys.concat(subkeys.map(subkey => `${_key}.${subkey}`)) | |
} | |
} | |
return keys | |
} | |
const docs = [ | |
// list of slate-editor state object | |
// pattern: { id: 000, doc: { ...stateObject } } | |
] | |
const parent = (path, level = 1) => path.split('.').slice(0, level * -1).join('.') | |
const legacy = doc => { | |
const links = deep(doc, 'type', 'link') | |
const legacyLinkPaths = [] | |
links.map(pathLink => { | |
const linkParentNode = _.get(doc, parent(pathLink)) | |
const hasHref = !!linkParentNode.data.href | |
if (!hasHref) legacyLinkPaths.push(pathLink) | |
}) | |
return legacyLinkPaths | |
} | |
const data = (doc, pathLinks) => pathLinks.map( | |
pathLink => { | |
const linkParentNode = _.get(doc, parent(pathLink)) | |
const textPath = deep(linkParentNode, 'text')[0] | |
const text = _.get(linkParentNode, textPath) | |
return { text } | |
} | |
) | |
const template = dataList => dataList.map(data => ({ | |
"kind": "range", | |
"text": data.text, | |
"marks": [] | |
})) | |
const update = (doc, linkPaths, templates) => { | |
linkPaths.map((linkPath, i) => { | |
const nodeIndex = Number(parent(linkPath).split('.').slice(-1)[0]) | |
const prevNodeIndex = !isNaN(nodeIndex) ? Number(nodeIndex) - 1 : 0 | |
const prevNodePath = `${parent(linkPath, 2)}.${prevNodeIndex}` | |
const nodesPath = parent(linkPath, 2) | |
if (_.get(doc, nodesPath).constructor === Array) { | |
_.update(doc, nodesPath, nodes => [ | |
...nodes.slice(0, nodeIndex), | |
...nodes.slice(nodeIndex + 1) | |
]) | |
} | |
_.update(doc, prevNodePath, node => { | |
node.ranges.push(templates[i]) | |
return node | |
}) | |
}) | |
return doc | |
} | |
const escapeIt = doc => | |
JSON.stringify(escapeJSON(JSON.stringify(doc))) | |
.replace(/\\\\\\\\\\/g, '\\\\\\') | |
.replace(/\$/g, '\\$') | |
const updateSQL = (id, escapedDoc) => | |
`UPDATE widgets SET settings = settings || ($$content => ${escapedDoc}$$) WHERE id = ${id};` | |
const print = (id, escapedDoc, linkPaths) => { | |
if (linkPaths.length) console.log(updateSQL(id, escapedDoc)) | |
} | |
docs.map(({ id, doc }) => { | |
const linkPaths = legacy(doc) | |
if (linkPaths.length) { | |
const dataList = data(doc, linkPaths) | |
const templates = template(dataList) | |
const updateDoc = update(doc, linkPaths, templates) | |
const escapedDoc = escapeIt(updateDoc) | |
print(id, escapedDoc, linkPaths) | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment