longdown
Version:
Convert longform markdown files to outline format used by Logseq
294 lines (246 loc) • 9.44 kB
JavaScript
import * as squint_core from 'squint-cljs/core.js';
import { defaultHandlers } from 'mdast-util-to-markdown';
import rehype_parse from 'rehype-parse';
import rehype_remark from 'rehype-remark';
import remark_parse from 'remark-parse';
import remark_stringify from 'remark-stringify';
import { unified } from 'unified';
import * as str from 'squint-cljs/src/squint/string.js';
var stratify_nodes = function (p__5) {
const vec__14 = p__5;
const seq__25 = squint_core.seq(vec__14);
const first__36 = squint_core.first(seq__25);
const seq__27 = squint_core.next(seq__25);
const node8 = first__36;
const remaining9 = seq__27;
const G__610 = squint_core.get(node8, "type");
switch (G__610) {case "heading":
const map__1213 = node8;
const depth14 = squint_core.get(map__1213, "depth");
let nodes15 = remaining9;
let children16 = [];
while(true){
const child17 = squint_core.first(nodes15);
if (squint_core.truth_((() => {
const and__23554__auto__18 = child17;
if (squint_core.truth_(and__23554__auto__18)) {
const or__23522__auto__19 = !(squint_core.get(child17, "type") === "heading");
if (or__23522__auto__19) {
return or__23522__auto__19} else {
return (depth14 < squint_core.get(child17, "depth"))};
} else {
return and__23554__auto__18};
})())) {
const vec__2023 = stratify_nodes(nodes15);
const nodes24 = squint_core.nth(vec__2023, 0, null);
const child25 = squint_core.nth(vec__2023, 1, null);
let G__26 = nodes24;
let G__27 = squint_core.conj(children16, child25);
nodes15 = G__26;
children16 = G__27;
continue;
} else {
return [nodes15, ({"type": "listItem", "children": [node8, ({"type": "list", "children": children16})]})]};
;break;
}
;
break;
case "list":
return [remaining9, ({"type": "listItem", "children": [({"type": "paragraph", "children": [({"type": "text", "text": ""})]}), node8]})];
break;
default:
return [remaining9, ({"type": "listItem", "children": [node8]})]};
};
var stratify = function (node) {
if ((squint_core.get(node, "type") === "root")) {
} else {
throw (new Error("Assert failed: (= (:type node) \"root\")"))};
let nodes1 = squint_core.get(node, "children");
let children2 = [];
while(true){
if (squint_core.truth_(squint_core.seq(nodes1))) {
const vec__36 = stratify_nodes(nodes1);
const nodes7 = squint_core.nth(vec__36, 0, null);
const child8 = squint_core.nth(vec__36, 1, null);
let G__9 = nodes7;
let G__10 = squint_core.conj(children2, child8);
nodes1 = G__9;
children2 = G__10;
continue;
} else {
return squint_core.assoc(node, "children", [({"type": "list", "children": children2})])};
;break;
}
;
};
var strip_leading_number = function (text) {
return str.replace(text, /^\d+\.\s*/, "");
};
var heading__GT_paragraph = function (node) {
return squint_core.update(squint_core.dissoc(squint_core.assoc(node, "type", "paragraph"), "depth"), "children", (function (children) {
return squint_core.mapv((function (child) {
if ((squint_core.get(child, "type") === "text")) {
return squint_core.update(child, "value", strip_leading_number)} else {
return child};
}), children);
}));
};
var empty_text_node_QMARK_ = function (node) {
return ((squint_core.get(node, "type") === "text") && squint_core.empty_QMARK_(squint_core.get(node, "value")));
};
var unwrap_leading_strong = function (children) {
const non_empty1 = squint_core.drop_while(empty_text_node_QMARK_, children);
const temp__23064__auto__2 = squint_core.first(non_empty1);
if (squint_core.truth_(temp__23064__auto__2)) {
const first_child3 = temp__23064__auto__2;
if ((squint_core.get(first_child3, "type") === "strong")) {
return squint_core.concat(squint_core.get(first_child3, "children"), squint_core.rest(non_empty1))} else {
return children};
} else {
return children};
};
var strip_leading_bold_in_node = function (node) {
if (squint_core.truth_(squint_core.contains_QMARK_((new Set (["paragraph", "listItem"])), squint_core.get(node, "type")))) {
return squint_core.update(node, "children", squint_core.comp(squint_core.vec, unwrap_leading_strong))} else {
return node};
};
var strip_highlights = function (node) {
const transformed1 = (((squint_core.get(node, "type") === "heading")) ? (strip_leading_bold_in_node(heading__GT_paragraph(node))) : ((((squint_core.get(node, "type") === "paragraph")) ? (strip_leading_bold_in_node(node)) : ((("else") ? (node) : (null))))));
if (squint_core.truth_(squint_core.get(transformed1, "children"))) {
return squint_core.update(transformed1, "children", (function (_PERCENT_1) {
return squint_core.mapv(strip_highlights, _PERCENT_1);
}))} else {
return transformed1};
};
var remark_stratify = function () {
return function (tree, _file, cb) {
const next_tree1 = stratify(tree);
return cb(null, next_tree1);
};
};
var remark_strip_highlights = function () {
return function (tree, _file, cb) {
const next_tree1 = strip_highlights(tree);
return cb(null, next_tree1);
};
};
var onenterlineprefix = function (token) {
const this$1 = this;
const node2 = this$1.stack[(this$1.stack.length - 1)];
if ((node2.type === "paragraph")) {
return this$1.config.enter.data.call(this$1, token);
};
};
var onexitlineprefix = function (token) {
const this$1 = this;
const node2 = this$1.stack[(this$1.stack.length - 1)];
if ((node2.type === "text")) {
return this$1.config.exit.data.call(this$1, token);
};
};
var preserve_leading_whitespace_extension = ({"enter": ({"linePrefix": onenterlineprefix}), "exit": ({"linePrefix": onexitlineprefix})});
var remark_preserve_leading_whitespace = function () {
const this$1 = this;
this$1.data("fromMarkdownExtensions", [preserve_leading_whitespace_extension]);
return null;
};
var remark_parse_longdown = function () {
const this$1 = this;
this$1.use(remark_parse).use(remark_preserve_leading_whitespace);
return null;
};
var filter_unsafe = function (unsafe_arr) {
return unsafe_arr.filter((function (x) {
return (squint_core.not(((x.inConstruct === "phrasing") && (() => {
const or__23522__auto__1 = (x.character === " ");
if (or__23522__auto__1) {
return or__23522__auto__1} else {
return (x.character === "\t")};
})())) && (!(x.character === "_") && (!(x.character === "&") && (!(x.character === "[") && !(x.character === "]")))));
}));
};
var custom_paragraph = function (node, _, state, info) {
const orig_unsafe1 = state.unsafe;
const _2 = state.unsafe = filter_unsafe(orig_unsafe1);
const value3 = defaultHandlers.paragraph(node, _2, state, info);
const _4 = state.unsafe = orig_unsafe1;
return value3;
};
var stringify_options = ({"bullet": "-", "listItemIndent": "one", "fences": true, "join": [(function (_left, _right, _parent, _state) {
return 0;
})], "handlers": ({"paragraph": custom_paragraph})});
var markdown_parser = unified().use(remark_parse_longdown).use(remark_stratify).freeze();
var html_parser = unified().use(rehype_parse).use(rehype_remark).use(remark_stratify).freeze();
var markdown_stringifier = unified().use(remark_stringify, stringify_options).freeze();
var parse_markdown = function (input) {
return markdown_parser.runSync(markdown_parser.parse(input));
};
var parse_html = function (input) {
return html_parser.runSync(html_parser.parse(input));
};
var stringify_markdown = function (ast) {
return `${markdown_stringifier.stringify(ast)??''}`;
};
var convert = function (parse_fn, input, opts) {
const ast1 = parse_fn(input);
const ast2 = ((squint_core.truth_((() => {
const and__23554__auto__3 = opts;
if (squint_core.truth_(and__23554__auto__3)) {
return opts.stripHighlights} else {
return and__23554__auto__3};
})())) ? (strip_highlights(ast1)) : (ast1));
return stringify_markdown(ast2);
};
var longform__GT_outline = (() => {
const f7 = (function (...args8) {
const G__91 = args8.length;
switch (G__91) {case 1:
return f7.cljs$core$IFn$_invoke$arity$1(args8[0]);
break;
case 2:
return f7.cljs$core$IFn$_invoke$arity$2(args8[0], args8[1]);
break;
default:
throw (new Error(`${"Invalid arity: "}${args8.length??''}`))};
});
f7.cljs$core$IFn$_invoke$arity$1 = (function (input) {
return convert(parse_markdown, input, null);
});
f7.cljs$core$IFn$_invoke$arity$2 = (function (input, opts) {
return convert(parse_markdown, input, opts);
});
f7.cljs$lang$maxFixedArity = 2;
return f7;
})();
var html__GT_outline = (() => {
const f10 = (function (...args11) {
const G__121 = args11.length;
switch (G__121) {case 1:
return f10.cljs$core$IFn$_invoke$arity$1(args11[0]);
break;
case 2:
return f10.cljs$core$IFn$_invoke$arity$2(args11[0], args11[1]);
break;
default:
throw (new Error(`${"Invalid arity: "}${args11.length??''}`))};
});
f10.cljs$core$IFn$_invoke$arity$1 = (function (input) {
return convert(parse_html, input, null);
});
f10.cljs$core$IFn$_invoke$arity$2 = (function (input, opts) {
return convert(parse_html, input, opts);
});
f10.cljs$lang$maxFixedArity = 2;
return f10;
})();
var make_converter = function (p__13) {
const map__12 = p__13;
const html3 = squint_core.get(map__12, "html");
const strip_highlights_QMARK_4 = squint_core.get(map__12, "strip-highlights?");
const convert_fn5 = ((squint_core.truth_(html3)) ? (html__GT_outline) : (longform__GT_outline));
const opts6 = ((squint_core.truth_(strip_highlights_QMARK_4)) ? (({"stripHighlights": true})) : (null));
return function (input) {
return convert_fn5(input, opts6);
};
};
export { parse_html, custom_paragraph, stringify_markdown, parse_markdown, stringify_options, make_converter, longform__GT_outline, remark_stratify, remark_strip_highlights, remark_parse_longdown, stratify, strip_highlights, html__GT_outline }