UNPKG

longform-markdown-splitter

Version:

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

83 lines (82 loc) 3.94 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_Splitter_Transformer = void 0; var md_collection_1 = require("../md-collection"); var md_exporter_1 = require("../md-exporter"); var md_filesystem_1 = require("../md-filesystem"); var md_frontmatter_1 = require("../md-frontmatter"); var md_transformer_1 = require("../md-transformer"); var MD_Splitter_Transformer = (function (_super) { __extends(MD_Splitter_Transformer, _super); function MD_Splitter_Transformer(parameter) { var _this = _super.call(this) || this; _this.collection = null; _this.counter = 0; _this.parameter = parameter; _this.parameter.frontmatter_filename = parameter.frontmatter_filename.trim(); if (parameter.frontmatter_filename.length > 0 && md_filesystem_1.MD_Filesystem.is_file_exist(parameter.frontmatter_filename)) { var frontmatter = new md_frontmatter_1.MD_Frontmatter_Template(""); frontmatter.load(parameter.frontmatter_filename); parameter.frontmatter = frontmatter; } return _this; } MD_Splitter_Transformer.prototype.set_job_parameter = function (job_paramter) { _super.prototype.set_job_parameter.call(this, job_paramter); }; MD_Splitter_Transformer.prototype.addObserver = function (observer) { _super.prototype.addObserver.call(this, observer); }; MD_Splitter_Transformer.prototype.transform = function (file_content, index) { if (file_content.body_array[index].indexOf(this.parameter.pattern) == 0) { this.counter = this.counter + 1; if (typeof this.parameter.limit != "undefined" && this.counter > this.parameter.limit) return file_content; if (this.collection !== null) { this.collection.write_file(this.job_parameter.writePath); } var params = { split_row: file_content.body_array[index], cleanName: this.parameter.cleanName, weightBase: this.parameter.weightBase, url_prefix: this.parameter.url_prefix, frontmatter: this.parameter.frontmatter, useCounter: this.parameter.hasCounter, counter: this.counter, }; this.collection = new md_collection_1.MD_Collection(params); if (this.parameter.doRemoveHeadline) file_content.body_array.splice(index, 1); } else { if (this.collection !== null) { this.collection.add_content(file_content.body_array[index]); } } if (this.collection !== null && index == file_content.body_array.length - 1) { this.collection.write_file(this.job_parameter.writePath); } this.observer_subject.notify_all("md-splitter-task", "md-exporter", md_exporter_1.MD_EXPORTER_COMMANDS.DO_NOT_WRITE_FILES); return file_content; }; return MD_Splitter_Transformer; }(md_transformer_1.MD_Transformer_AbstractBase)); exports.MD_Splitter_Transformer = MD_Splitter_Transformer;