UNPKG

longform-markdown-splitter

Version:

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

105 lines (104 loc) 4.95 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_MathInline_Transformer = exports.MD_MathParagraph_Transformer = void 0; var md_template_1 = require("../md-template"); var md_transformer_1 = require("../md-transformer"); var MD_MathParagraph_Transformer = (function (_super) { __extends(MD_MathParagraph_Transformer, _super); function MD_MathParagraph_Transformer(parameter) { var _this = _super.call(this) || this; _this.collection = null; _this.counter = 0; _this.doCollect = false; _this.template_values = { content: "", }; _this.parameter = parameter; return _this; } MD_MathParagraph_Transformer.prototype.set_job_parameter = function (job_paramter) { _super.prototype.set_job_parameter.call(this, job_paramter); }; MD_MathParagraph_Transformer.prototype.addObserver = function (observer) { _super.prototype.addObserver.call(this, observer); }; MD_MathParagraph_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.log("#### math: found!"); this.collection = []; file_content.body_array.splice(index, 1); file_content.index = file_content.index - 1; } else if (this.doCollect) { if (item.indexOf(this.parameter.tag_obsidian_suffix) >= 0) { this.doCollect = false; console.log("#### math: process!"); 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); } else { this.collection.push(item); file_content.body_array.splice(index, 1); file_content.index = file_content.index - 1; console.log("#### math: collect...", this.collection); } } return file_content; }; return MD_MathParagraph_Transformer; }(md_transformer_1.MD_Transformer_AbstractBase)); exports.MD_MathParagraph_Transformer = MD_MathParagraph_Transformer; var MD_MathInline_Transformer = (function (_super) { __extends(MD_MathInline_Transformer, _super); function MD_MathInline_Transformer(parameter) { var _this = _super.call(this) || this; _this.template_values = { content: "", }; _this.parameter = parameter; return _this; } MD_MathInline_Transformer.prototype.set_job_parameter = function (job_paramter) { _super.prototype.set_job_parameter.call(this, job_paramter); }; MD_MathInline_Transformer.prototype.addObserver = function (observer) { _super.prototype.addObserver.call(this, observer); }; MD_MathInline_Transformer.prototype.transform = function (file_content, index) { var paragraph = file_content.body_array[index]; var words_array = paragraph.split(" "); for (var i = 0; i < words_array.length; i++) { var word = words_array[i].trim(); if (word.startsWith(this.parameter.tag_obsidian_prefix) && word.endsWith(this.parameter.tag_obsidian_suffix)) { this.template_values.content = word.substring(this.parameter.tag_obsidian_prefix.length, word.length - this.parameter.tag_obsidian_suffix.length); console.log("found inline formula!", this.template_values.content); var template = new md_template_1.MD_Template(this.parameter.replace_template); words_array[i] = template.fill(this.template_values); } } file_content.body_array[index] = words_array.join(" "); return file_content; }; return MD_MathInline_Transformer; }(md_transformer_1.MD_Transformer_AbstractBase)); exports.MD_MathInline_Transformer = MD_MathInline_Transformer;