102 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 '../..';
2022-11-21 20:11:20 -08:00
import EmojiIconModel from '../model/EmojiIconModel';
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();
2022-01-11 17:52:54 -08:00
2022-11-24 16:51:30 -08:00
const centralTopicText = centralTopic.getText();
let result = '';
if (centralTopicText) {
const centralText = this.normalizeText(centralTopicText);
2022-01-11 17:52:54 -08:00
2022-11-24 16:51:30 -08:00
// Traverse all the branches ...
result = `# ${centralText}\n\n`;
result += this.traverseBranch('', centralTopic.getChildren());
2022-01-11 17:52:54 -08:00
2022-11-24 16:51:30 -08:00
// White footnotes:
if (this.footNotes.length > 0) {
result += '\n\n\n';
this.footNotes.forEach((note, index) => {
result += `[^${index + 1}]: ${this.normalizeText(note)}`;
});
}
result += '\n';
}
2022-09-15 21:27:34 -07:00
return Promise.resolve(result);
}
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) => {
2022-11-21 20:11:20 -08:00
// Convert icons to list ...
const icons = node.getFeatures().filter((f) => f.getType() === 'eicon');
let iconStr = ' ';
if (icons.length > 0) {
iconStr = ` ${icons.map((icon) => (icon as EmojiIconModel).getIconType()).toString()} `;
}
result = `${result}${prefix}-${iconStr}${node.getText()}`;
2022-01-26 14:19:14 -08:00
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}] `;
}
});
result = `${result}\n`;
2022-01-11 17:52:54 -08:00
2022-11-27 10:22:19 -08:00
if (node.getChildren().filter((n) => n.getText() !== null).length > 0) {
2022-01-26 14:19:14 -08:00
result += this.traverseBranch(`${prefix}\t`, node.getChildren());
}
});
return result;
}
2022-01-11 17:52:54 -08:00
}
export default MDExporter;