@mieweb/wikigdrive
Version:
Google Drive to MarkDown synchronization
61 lines (60 loc) • 1.93 kB
JavaScript
import { fixCharacters } from './utils.js';
import { chunksToText } from './markdownNodesUtils.js';
export class MarkdownNodes {
constructor() {
Object.defineProperty(this, "body", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
this.body = this.createNode('BODY', {});
}
createNode(tag, payload = {}) {
const node = {
isTag: true,
tag,
payload,
children: []
};
const oldSplice = node.children.splice;
node.children.splice = function (start, deleteCount, ...items) {
const retVal = oldSplice.apply(node.children, [start, deleteCount, ...items]);
// const retVal = oldSplice(start, deleteCount, ...items);
for (let idx = 0; idx < items.length; idx++) {
items[idx].parent = node;
}
return retVal;
};
return node;
}
toString() {
return chunksToText(this.body.children, { mode: 'md', addLiIndents: true })
.split('\n')
.map(line => line.trim().length > 0 ? line : '')
.join('\n');
}
replace(start, end, chunk) {
const deleteCount = end - start + 1;
this.body.children.splice(start, deleteCount, chunk);
for (let i = start; i < this.body.children.length; i++) {
const chunk = this.body.children[i];
if (chunk.isTag) {
chunk.payload.position -= deleteCount;
}
}
}
append(parent, child) {
parent.children.push(child);
child.parent = parent;
}
appendText(parent, txt) {
txt = fixCharacters(txt);
parent.children.push({
isTag: false,
text: txt,
parent,
comment: 'MarkdownNodes.ts: appendText'
});
}
}