@mieweb/wikigdrive
Version:
Google Drive to MarkDown synchronization
76 lines (75 loc) • 2.44 kB
JavaScript
import { Transform } from 'node:stream';
import { XmlDocument } from 'xmldoc';
import { urlToFolderId } from './utils/idParsers.js';
export class SvgTransform extends Transform {
constructor(localPath) {
super();
Object.defineProperty(this, "localPath", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "content", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "links", {
enumerable: true,
configurable: true,
writable: true,
value: new Set()
});
this.localPath = localPath;
this.content = '';
}
_transform(chunk, encoding, callback) {
if (encoding === 'buffer') {
chunk = chunk.toString();
}
this.content += chunk;
callback();
}
addLink(href) {
if (href && !href.startsWith('#') && href.indexOf(':') > -1) {
this.links.add(href);
}
}
async _flush(callback) {
const document = new XmlDocument(this.content);
const urlToRelativePath = {};
const findLinkInChild = (child) => {
if (child.attr['xlink:href']) {
const fileId = urlToFolderId(child.attr['xlink:href']);
if (fileId) {
this.addLink('gdoc:' + fileId);
child.attr['xlink:href'] = 'gdoc:' + fileId;
}
urlToRelativePath[child.attr['xlink:href']] = null;
}
child.eachChild((child) => {
findLinkInChild(child);
});
};
document.eachChild((child) => {
findLinkInChild(child);
});
const replaceLinkInChild = (child) => {
if (child.attr['xlink:href']) {
if (urlToRelativePath[child.attr['xlink:href']]) {
child.attr['xlink:href'] = urlToRelativePath[child.attr['xlink:href']];
}
}
child.eachChild((child) => {
replaceLinkInChild(child);
});
};
document.eachChild((child) => {
replaceLinkInChild(child);
});
this.push(document.toString());
callback();
}
}