🔬 Push, Pull and Extract

Table of Contents

Test cases

The current implementation (due to ast2md) causes Table of Contents, wiki-links and normal links to be saved incorrectly, as well as transcluded code:

text< ../dummy/🔥.lit
🔥 Emoji in file name. ⚠️

Push & Pull

Increase or decrease a sections nesting level.

js
const {visit, selectPosition} = lit.utils.unist
const {toMarkdown, ungroupSections}
      = lit.parser.utils
const selectPos = selectPosition.selectAll
const unGroup = ungroupSections()()
const ast2md = tree => toMarkdown(unGroup(tree))

const tree = lit.ast
const test = 'heading'
const push = (node) => node.depth += 1 
const pull = (node) => node.depth -= 1

visit(tree,test,push)

// return selectPos.toString()
return ast2md(tree)
jsvisit-position
const { visit, filter, select, selectPosition } = lit.utils.unist;

let found;

const { toMarkdown, ungroupSections } = lit.parser.utils;

const ast2md = (ast) => {
  const unGroup = ungroupSections()();
  const tree = unGroup(ast);
  const md = toMarkdown(tree);
  return md;
};

const secFromCell = (pos) => {
  const secs = selectPosition.selectAll("section", pos, lit.ast);
  const last = secs.slice(-1)[0];
  return last.position;
};

const pos = secFromCell(this.position);

const withPos = (tree, pos, visitor) =>
  visit(
    lit.ast,
    () => true,
    (node) => {
      if (node?.position?.start?.offset === pos.start?.offset) {
        console.log("found match: ", node.type);
        visitor(node);
        return visit.SKIP;
      }
    }
  );

withPos(lit.ast, pos, (node) => {
  visit(node, "heading", (node) => {
    node.depth += 1;
  });
});

return ast2md(lit.ast);

As menu !plugin

js../plugins/menu/depth.jspluginid=depthtype=sectionmenu
export const sectionmenu = (ctx, { React, Menu }) => {
  const rc = React.createElement;
  const { visit } = lit.utils.unist;

  const update = () => ctx.setSrc(lit.ast.position, ctx.ast2md(lit.ast));

  const withPos = (tree, pos, visitor) =>
    visit(
      lit.ast,
      () => true,
      (node) => {
        if (node?.position?.start?.offset === pos.start?.offset) {
          console.log("found match: ", node.type);
          visitor(node);
          return visit.SKIP;
        }
      }
    );

  const push = () =>
    withPos(lit.ast, ctx.selectedCell, (node) => {
      visit(node, "heading", (node) => (node.depth += 1));
      update();
    });

  const pull = () =>
    withPos(lit.ast, ctx.selectedCell, (node) => {
      visit(node, "heading", (node) => (node.depth -= 1));
      update();
    });

  const Push = rc("span", { onClick: push }, "Push");

  const Pull = rc("span", { onClick: pull }, "Pull");

  return rc(
    Menu,
    {
      title: "Depth",
      disabled: !ctx.selectedCell,
    },
    [Push, Pull]
  );
};

Extract

Other

jsvisit-headingspush-all
const { visit } = lit.utils.unist;
const { toMarkdown, ungroupSections } = lit.parser.utils;

const ast2md = (ast) => {
  const unGroup = ungroupSections()();
  const tree = unGroup(ast);
  const md = toMarkdown(tree);
  return md;
};

visit(lit.ast, "heading", (node) => {
  node.depth += 1;
});

return ast2md(lit.ast);