UNPKG

longform-markdown-splitter

Version:

Splits and transforms markdown files from obsidian for usage in hugo.

157 lines (156 loc) 6.23 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.MD_Callout_Transformer = void 0; var md_template_1 = require("../md-template"); var md_transformer_1 = require("../md-transformer"); var MD_Callout_Transformer = (function (_super) { __extends(MD_Callout_Transformer, _super); function MD_Callout_Transformer(parameter) { var _this = _super.call(this) || this; _this.collection = null; _this.counter = 0; _this.doCollect = false; _this.template_values = { context: "", title: "", icon: "", content: "", }; _this.a_mapping = { source_field_name: "type", target_field_name: "context", transformations: [ { "note, seealso, example, quote, cite": "note" }, { "info, todo, tip": "tip" }, { "warning,caution, attention": "caution" }, { "danger, failure, fail, missing": "danger" }, ], }; _this.b_mapping = { source_field_name: "title", target_field_name: "title", transformations: "{value}", }; _this.c_mapping = { source_field_name: "icon", target_field_name: "title", transformations: "{value}", }; _this.mappings = [ { "note, seealso": { context: "", icon: "", }, "abstract, summary, tldr": { context: "", icon: "", }, "info, todo": { context: "", icon: "", }, "tip, hint, important": { context: "", icon: "", }, "success, check, done": { context: "", icon: "", }, "question, help, faq": { context: "", icon: "", }, " warning, caution, attention": { context: "", icon: "", }, "failure, fail, missing": { context: "", icon: "", }, "danger, error": { context: "", icon: "", }, bug: { context: "", icon: "", }, example: { context: "", icon: "", }, "quote, cite": { context: "", icon: "", }, }, ]; _this.parameter = parameter; return _this; } MD_Callout_Transformer.prototype.set_job_parameter = function (job_paramter) { _super.prototype.set_job_parameter.call(this, job_paramter); }; MD_Callout_Transformer.prototype.addObserver = function (observer) { _super.prototype.addObserver.call(this, observer); }; MD_Callout_Transformer.prototype.transform = function (file_content, index) { var item = file_content.body_array[index]; if (!this.doCollect && item.indexOf(this.parameter.tag_obsidian_prefix) >= 0) { this.doCollect = true; console.group('callout task'); console.log("###########################################"); console.log("###########################################"); console.log("#### callout: found!"); this.collection = []; var separator_pos = item.indexOf(this.parameter.tag_obsidian_suffix); this.template_values.context = item.substring(this.parameter.tag_obsidian_prefix.length, separator_pos); this.template_values.title = item.substring(separator_pos + 1, item.length).trim(); file_content.body_array.splice(index, 1); file_content.index = file_content.index - 1; } else if (this.doCollect) { var token_pos = item.indexOf(">"); if (token_pos >= 0) { var next_item = file_content.body_array[index + 1]; if (next_item.indexOf(">") >= 0) { this.collection.push(item.substring(token_pos + 1, item.length).trim()); file_content.body_array.splice(index, 1); file_content.index = file_content.index - 1; } } else { this.doCollect = false; console.log("#### callout: process!", this.collection, this.template_values); console.log("###########################################"); console.log("###########################################"); console.groupEnd(); this.template_values.content = this.collection.join("\n"); var template = new md_template_1.MD_Template(this.parameter.replace_template); file_content.body_array[index] = template.fill(this.template_values); } } return file_content; }; return MD_Callout_Transformer; }(md_transformer_1.MD_Transformer_AbstractBase)); exports.MD_Callout_Transformer = MD_Callout_Transformer;