longform-markdown-splitter
Version:
Splits and transforms markdown files from obsidian for usage in hugo.
98 lines (97 loc) • 5.37 kB
JavaScript
"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_ObsidianLink_Transformer = exports.MD_ObsidianLink_Transformer_Base = void 0;
var md_copy_job_1 = require("../md-copy-job");
var md_template_1 = require("../md-template");
var md_transformer_1 = require("../md-transformer");
var MD_ObsidianLink_Transformer_Base = (function (_super) {
__extends(MD_ObsidianLink_Transformer_Base, _super);
function MD_ObsidianLink_Transformer_Base(parameter) {
var _this = _super.call(this) || this;
_this.tag_obsidian_prefix = "";
_this.tag_obsidian_suffix = "";
_this.find_rule = "";
_this.replace_template = "";
_this.tag = "";
_this.template_values = {
name_suffix: "",
name: "",
name_full: "",
};
_this.tag_obsidian_prefix = parameter.tag_obsidian_prefix;
_this.tag_obsidian_suffix = parameter.tag_obsidian_suffix;
_this.replace_template = parameter.replace_template;
_this.find_rule = parameter.find_rule;
_this.copy_task = parameter.copy_task;
return _this;
}
MD_ObsidianLink_Transformer_Base.prototype.reset = function () {
this.tag = "";
this.template_values.name_full = "";
this.template_values.name = "";
this.template_values.name_suffix = "";
};
MD_ObsidianLink_Transformer_Base.prototype.set_job_parameter = function (job_paramter) {
_super.prototype.set_job_parameter.call(this, job_paramter);
};
MD_ObsidianLink_Transformer_Base.prototype.transform = function (file_content, index) {
var item = file_content.body_array[index];
this.reset();
if (item.indexOf(this.tag_obsidian_prefix) >= 0) {
this.tag = item.substring(item.indexOf(this.tag_obsidian_prefix), item.lastIndexOf(this.tag_obsidian_suffix) + 2);
this.template_values.name_full = item.substring(item.indexOf(this.tag_obsidian_prefix) + 3, item.lastIndexOf(this.tag_obsidian_suffix));
this.template_values.name = this.template_values.name_full.substring(0, this.template_values.name_full.lastIndexOf("."));
this.template_values.name_suffix =
this.template_values.name_full.substring(this.template_values.name_full.lastIndexOf(".") + 1, this.template_values.name_full.length);
}
return file_content;
};
MD_ObsidianLink_Transformer_Base.prototype.toString = function (what) {
return "\n Transform ".concat(what, "...\n tag : ").concat(this.tag, "\n name_full : ").concat(this.template_values.name_full, "\n name : ").concat(this.template_values.name, "\n name_suffix : ").concat(this.template_values.name_suffix, "\n copy_task : ").concat(md_copy_job_1.MD_CopyJob.toString(this));
};
return MD_ObsidianLink_Transformer_Base;
}(md_transformer_1.MD_Transformer_AbstractBase));
exports.MD_ObsidianLink_Transformer_Base = MD_ObsidianLink_Transformer_Base;
var MD_ObsidianLink_Transformer = (function (_super) {
__extends(MD_ObsidianLink_Transformer, _super);
function MD_ObsidianLink_Transformer() {
return _super !== null && _super.apply(this, arguments) || this;
}
MD_ObsidianLink_Transformer.prototype.add_observer = function (observer) {
this.observer_subject.add_observer(observer);
};
MD_ObsidianLink_Transformer.prototype.transform = function (file_content, index) {
_super.prototype.transform.call(this, file_content, index);
if (this.template_values.name_suffix.match("^(".concat(this.find_rule, ")$"))) {
console.log(this.toString("find rule '".concat(this.find_rule, "'")));
console.log("item before : ".concat(file_content.body_array[index]));
var hugo_template = new md_template_1.MD_Template(this.replace_template);
file_content.body_array[index] = file_content.body_array[index].replace(this.tag, hugo_template.fill(this.template_values));
console.log("item after : ".concat(file_content.body_array[index]));
console.log("");
if (md_copy_job_1.MD_CopyJob.hasCopyTask(this)) {
this.copy_task.simulate =
this.copy_task.simulate && this.job_parameter.simulate;
md_copy_job_1.MD_CopyJob.perform(this.copy_task, this.template_values);
}
}
return file_content;
};
return MD_ObsidianLink_Transformer;
}(MD_ObsidianLink_Transformer_Base));
exports.MD_ObsidianLink_Transformer = MD_ObsidianLink_Transformer;