@formant/ava-react
Version:
React components of AVA.
248 lines (246 loc) • 9.31 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/NarrativeTextVis/chore/exporter/TextExporter.ts
var TextExporter_exports = {};
__export(TextExporter_exports, {
TextExporter: () => TextExporter
});
module.exports = __toCommonJS(TextExporter_exports);
var import_ava = require("@formant/ava");
var import_lodash = require("lodash");
var import_plugin = require("../plugin");
var import_constants = require("../../constants");
var import_transformHtml = require("./helpers/transformHtml");
var NTV_CONTAINER_CLS = `${import_constants.NTV_PREFIX_CLS}-container`;
var TextExporter = class extends import_plugin.PluginManager {
/**
* Export your text schema as pure text.
* @param {NarrativeTextSpec} spec - the narrative text schema you want to export
* @returns
*/
getNarrativeText(spec) {
var _a, _b;
let text = "";
if ((_a = spec == null ? void 0 : spec.headline) == null ? void 0 : _a.phrases)
text += this.getPhrasesText(spec.headline.phrases);
if (spec == null ? void 0 : spec.sections) {
text = (_b = spec == null ? void 0 : spec.sections) == null ? void 0 : _b.reduce((prev, curr) => `${prev}${prev ? "\r\n" : ""}${this.getSectionText(curr)}`, text);
}
return text;
}
getSectionText(spec) {
if ((0, import_ava.isStandardSection)(spec)) {
return spec.paragraphs.reduce((prev, curr) => `${prev}\r
${this.getParagraphText(curr)}`, "");
}
if ((0, import_ava.isCustomSection)(spec)) {
const descriptor = this.getBlockDescriptor(spec.customType);
if (descriptor && (descriptor == null ? void 0 : descriptor.getText)) {
return descriptor.getText(spec);
}
}
return "";
}
getParagraphText(spec) {
var _a;
if ((0, import_ava.isHeadingParagraph)(spec) || (0, import_ava.isTextParagraph)(spec))
return this.getPhrasesText(spec.phrases);
if ((0, import_ava.isBulletParagraph)(spec)) {
const level = 1;
return (_a = spec.bullets) == null ? void 0 : _a.reduce(
(prev, curr, index) => (
// if is ordered bullet, add '1. xxxxxx'
// else, add '· xxxxxx'
`${prev}${prev ? "\r\n" : ""}${spec.isOrder ? `${index + 1}. ` : "· "}${this.getBulletsText(curr, level)}`
),
""
);
}
if ((0, import_ava.isCustomParagraph)(spec)) {
const descriptor = this.getBlockDescriptor(spec.customType);
if (descriptor && (descriptor == null ? void 0 : descriptor.getText)) {
return descriptor.getText(spec);
}
}
return "";
}
getBulletsText(spec, level = 1) {
var _a;
let text = "";
if (spec == null ? void 0 : spec.phrases) {
text = this.getPhrasesText(spec.phrases);
}
if (spec == null ? void 0 : spec.subBullet) {
text = (_a = spec.subBullet.bullets) == null ? void 0 : _a.reduce(
(prev, curr, index) => (
// if is ordered bullet, add paddings + '1. xxxxxx'
// else, add paddings + '· xxxxxx'
// padding number here is 2
`${prev}\r
${(0, import_lodash.pad)("", level * 2)}${spec.subBullet.isOrder ? `${index + 1}. ` : "· "}${this.getBulletsText(
curr,
level + 1
)}`
),
text
);
}
return text;
}
getPhrasesText(spec) {
return spec.reduce((prev, curr) => {
let text = curr == null ? void 0 : curr.value;
if ((0, import_ava.isEntityPhrase)(curr) || (0, import_ava.isCustomPhrase)(curr)) {
const descriptor = this.getPhraseDescriptorBySpec(curr);
if (descriptor && (descriptor == null ? void 0 : descriptor.getText)) {
text = descriptor.getText(curr.value, curr.metadata);
}
}
return prev + text;
}, "");
}
/**
* Export your text schema as Markdown.
* @param {NarrativeTextSpec} spec - the narrative text schema you want to export
* @returns
*/
getNarrativeMarkdown(spec) {
var _a, _b;
let text = "";
if ((_a = spec == null ? void 0 : spec.headline) == null ? void 0 : _a.phrases)
text += `# ${this.getPhrasesMarkdown(spec.headline.phrases)}\r
\r
---`;
if (spec == null ? void 0 : spec.sections) {
text = (_b = spec == null ? void 0 : spec.sections) == null ? void 0 : _b.reduce(
(prev, curr) => `${prev}${prev ? "\r\n" : ""}${this.getSectionMarkdown(curr)}`,
text
);
}
return text;
}
getSectionMarkdown(spec) {
if ((0, import_ava.isStandardSection)(spec)) {
return spec.paragraphs.reduce((prev, curr) => `${prev}\r
${this.getParagraphMarkdown(curr)}`, "");
}
if ((0, import_ava.isCustomSection)(spec)) {
const descriptor = this.getBlockDescriptor(spec.customType);
if (descriptor == null ? void 0 : descriptor.getMarkdown) {
return descriptor.getMarkdown(spec);
}
if (descriptor == null ? void 0 : descriptor.getText) {
return descriptor.getText(spec);
}
}
return "";
}
getParagraphMarkdown(spec) {
var _a;
if ((0, import_ava.isTextParagraph)(spec))
return this.getPhrasesMarkdown(spec.phrases);
if ((0, import_ava.isHeadingParagraph)(spec)) {
return `${"#".repeat((0, import_ava.getHeadingWeight)(spec.type))} ${this.getPhrasesMarkdown(spec.phrases)}`;
}
if ((0, import_ava.isBulletParagraph)(spec)) {
const level = 1;
return (_a = spec.bullets) == null ? void 0 : _a.reduce(
(prev, curr, index) => (
// if is ordered bullet, add '1. xxxxxx'
// else, add '- xxxxxx'
`${prev}${prev ? "\r\n" : ""}${spec.isOrder ? `${index + 1}. ` : "- "}${this.getBulletsMarkdown(
curr,
level
)}`
),
""
);
}
if ((0, import_ava.isCustomParagraph)(spec)) {
const descriptor = this.getBlockDescriptor(spec.customType);
if (descriptor == null ? void 0 : descriptor.getMarkdown) {
return descriptor.getMarkdown(spec);
}
if (descriptor == null ? void 0 : descriptor.getText) {
return descriptor.getText(spec);
}
}
return "";
}
getBulletsMarkdown(spec, level = 1) {
var _a;
let text = "";
if (spec == null ? void 0 : spec.phrases) {
text = this.getPhrasesMarkdown(spec.phrases);
}
if (spec == null ? void 0 : spec.subBullet) {
text = (_a = spec.subBullet.bullets) == null ? void 0 : _a.reduce(
(prev, curr, index) => (
// if is ordered bullet, add paddings + '1. xxxxxx'
// else, add paddings + '- xxxxxx'
// padding number in markdown should be 4
`${prev}\r
${(0, import_lodash.pad)("", level * 4)}${spec.subBullet.isOrder ? `${index + 1}. ` : "- "}${this.getBulletsMarkdown(
curr,
level + 1
)}`
),
text
);
}
return text;
}
// TODO: in getPhrasesMarkdown, support styles defined by entity type and user customization
getPhrasesMarkdown(spec) {
return spec.reduce((prev, curr) => {
let text = curr == null ? void 0 : curr.value;
if ((0, import_ava.isEntityPhrase)(curr) || (0, import_ava.isCustomPhrase)(curr)) {
const descriptor = this.getPhraseDescriptorBySpec(curr);
if (descriptor == null ? void 0 : descriptor.getMarkdown) {
return descriptor.getMarkdown(curr.value, curr.metadata);
}
if (descriptor == null ? void 0 : descriptor.getText) {
return descriptor.getText(curr.value, curr.metadata);
}
} else {
if ("bold" in curr && curr.bold) {
text = `**${text}**`;
}
if ("italic" in curr && curr.italic) {
text = `*${text}*`;
}
}
return prev + text;
}, "");
}
/**
* get html string of narrative-vis content in container, the svg and canvas elements are converted to image by default
* @param container the container of narrative-vis content
* @param imageExtra the jump link to add to the image element
* @param replaceType replace the svg and canvas elements with image or text, so that they can be copied and pasted into some rich-text editors
*/
async getNarrativeHtml(container, imageExtra, replaceType = "image") {
const elements = container == null ? void 0 : container.getElementsByClassName(NTV_CONTAINER_CLS);
const transformedHtml = await (0, import_transformHtml.transformHtml)({ elements, imageExtra, replaceType });
return transformedHtml;
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
TextExporter
});