@subsocial/utils
Version:
JavaScript utils for Subsocial blockchain.
43 lines (42 loc) • 1.51 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.summarizeMd = exports.summarize = void 0;
const string_1 = require("./string");
const lodash_truncate_1 = __importDefault(require("lodash.truncate"));
const md_1 = require("./md");
const DEFAULT_SUMMARY_LEN = 300;
const SEPARATOR = /[.,:;!?()[\]{}\s]+/;
/** Shorten a plain text up to `limit` chars. Split by separators. */
const summarize = (text, opts = {}) => {
if ((0, string_1.isEmptyStr)(text))
return '';
text = text.trim();
const { limit = DEFAULT_SUMMARY_LEN, omission = '...' } = opts;
return text.length <= limit
? text
: (0, lodash_truncate_1.default)(text, {
length: limit,
separator: SEPARATOR,
omission
});
};
exports.summarize = summarize;
/**
* Convert markdown to plain text and shorten it up to `limit` chars.
*
* @returns `summary` and `isShowMore` flag which is `true` if the original text is longer than the summary.
*/
const summarizeMd = (md, opts = {}) => {
var _a;
const text = ((_a = (0, md_1.mdToText)(md)) === null || _a === void 0 ? void 0 : _a.trim()) || '';
const summary = (0, exports.summarize)(text, opts);
const isShowMore = text.length > summary.length;
return {
summary,
isShowMore
};
};
exports.summarizeMd = summarizeMd;