98 lines
3.2 KiB
TypeScript
Raw Normal View History

2022-01-11 17:52:54 -08:00
/*
* Copyright [2021] [wisemapping]
*
* Licensed under WiseMapping Public License, Version 1.0 (the "License").
* It is basically the Apache License, Version 2.0 (the "License") plus the
* "powered by wisemapping" text requirement on every single page;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the license at
*
* http://www.wisemapping.org/license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Mindmap } from '../..';
import INodeModel from '../model/INodeModel';
import LinkModel from '../model/LinkModel';
import NoteModel from '../model/NoteModel';
import Exporter from './Exporter';
2022-01-11 17:52:54 -08:00
2022-02-01 19:13:44 -08:00
class MDExporter extends Exporter {
private mindmap: Mindmap;
2022-01-11 17:52:54 -08:00
2022-01-26 14:19:14 -08:00
private footNotes: string[] = [];
2022-01-11 17:52:54 -08:00
constructor(mindmap: Mindmap) {
2022-02-01 19:13:44 -08:00
super('md', 'text/markdown');
this.mindmap = mindmap;
}
2022-01-11 17:52:54 -08:00
private normalizeText(value: string): string {
return value.replace('\n', '');
}
2022-01-11 17:52:54 -08:00
export(): Promise<string> {
this.footNotes = [];
2022-01-11 17:52:54 -08:00
// Add cental node as text ...
const centralTopic = this.mindmap.getCentralTopic();
const centralText = this.normalizeText(centralTopic.getText());
2022-01-11 17:52:54 -08:00
// Traverse all the branches ...
let result = `# ${centralText}\n\n`;
result += this.traverseBranch('', centralTopic.getChildren());
2022-01-11 17:52:54 -08:00
// White footnotes:
if (this.footNotes.length > 0) {
result += '\n\n\n';
this.footNotes.forEach((note, index) => {
result += `[^${index + 1}]: ${this.normalizeText(note)}`;
});
2022-01-11 17:52:54 -08:00
}
result += '\n';
2022-01-11 17:52:54 -08:00
2022-01-31 19:56:33 -08:00
// Encode as url response ...
const blob = new Blob([result], { type: 'text/markdown' });
const urlStr = URL.createObjectURL(blob);
return Promise.resolve(urlStr);
}
2022-01-11 17:52:54 -08:00
private traverseBranch(prefix: string, branches: Array<INodeModel>) {
let result = '';
2022-01-26 14:19:14 -08:00
branches
.filter((n) => n.getText() !== undefined)
.forEach((node) => {
result = `${result}${prefix}- ${node.getText()}`;
node.getFeatures().forEach((f) => {
const type = f.getType();
// Dump all features ...
if (type === 'link') {
result = `${result} ( [link](${(f as LinkModel).getUrl()}) )`;
}
2022-01-11 17:52:54 -08:00
2022-01-26 14:19:14 -08:00
if (type === 'note') {
const note = f as NoteModel;
this.footNotes.push(note.getText());
result = `${result}[^${this.footNotes.length}] `;
}
2022-01-11 17:52:54 -08:00
2022-01-26 14:19:14 -08:00
// if(type === 'icon'){
// const icon = f as IconModel;
// result = result + ` ![${icon.getIconType().replace('_','')}!](https://app.wisemapping.com/images/${icon.getIconType()}.svg )`
// }
});
result = `${result}\n`;
2022-01-11 17:52:54 -08:00
2022-01-26 14:19:14 -08:00
if (node.getChildren().filter((n) => n.getText() !== undefined).length > 0) {
result += this.traverseBranch(`${prefix}\t`, node.getChildren());
}
});
return result;
}
2022-01-11 17:52:54 -08:00
}
export default MDExporter;