Update pages in a tree

There are times you need to programmatically change documents under a given parent. The following is a bare bones way of using DA's public crawl function.

  • Javascript
// Import DA's public crawl function
import { crawl } from 'https://da.live/nx/public/utils/tree.js';

const path = '/geometrixx/outdoors/path/to/parent/to/traverse';

const callback = async (item) => {
  // Die if not a document
  if (!item.path.endsWith('.html')) return;

  // Fetch the doc & convert to DOM
  const resp = await fetch(`https://admin.da.live/source${item.path}`);
  if (!resp.ok) {
    console.log('Could not fetch item');
    return;
  }
  const text = await resp.text();
  const dom = new DOMParser().parseFromString(text, 'text/html');

  // MAKE
  // MAGIC
  // HAPPEN

  const html = dom.body.outerHTML;
  const data = new Blob([html], { type: 'text/html' });

  const body = new FormData();
  body.append('data', data);

  const opts = { method = 'POST', body };
  const { status } = await fetch(url, opts);
  console.log(status);
}

// Crawl the tree of content
const { results } = crawl({ path, callback, concurrent: 50 });
await results;

/