Files
site/node_modules/micromark-util-edit-map/dev/index.js
T
site-admin 3ac81d5a52
deploy / build-and-publish (push) Successful in 1m37s
the editable site (validation-lab)
2026-11-21 08:03:55 +01:00

238 lines
5.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* @import {Event} from 'micromark-util-types'
*/
/**
* @typedef {[number, number, Array<Event>]} Change
* @typedef {[number, number, number]} Jump
*/
/**
* Tracks a bunch of edits.
*
* Port of `edit_map.rs` from `markdown-rs`:
* <https://github.com/wooorm/markdown-rs/blob/1506572f/src/util/edit_map.rs>.
*
* Deal with several changes in events, batching them together.
*
* Preferably, changes should be kept to a minimum.
* Sometimes, it’s needed to change the list of events, because parsing can be
* messy, and it helps to expose a cleaner interface of events to the compiler
* and other users.
* It can also help to merge many adjacent similar events.
* And, in other cases, it’s needed to parse subcontent: pass some events
* through another tokenizer and inject the result.
*/
export class EditMap {
/**
* Create a new edit map.
*
* @returns
* New instance.
*/
constructor() {
/**
* Changes by index, so `add` does not need to scan `map` (which is
* quadratic for documents with many edits at different places).
*
* @type {Map<number, Change>}
*/
this.index = new Map()
/**
* Record of changes.
*
* @type {Array<Change>}
*/
this.map = []
}
/**
* Create an edit: a remove and/or add at a certain place.
*
* @param {number} index
* Index at which to apply the edit.
* @param {number} remove
* Count of items to remove at the index.
* @param {Array<Event>} add
* Items to add at the index.
* @returns {undefined}
* Nothing.
*/
add(index, remove, add) {
addImplementation(this, index, remove, add, false)
}
/**
* Create an edit: but insert `add` before existing additions, instead of
* after them.
*
* @param {number} index
* Index at which to apply the edit.
* @param {number} remove
* Count of items to remove at the index.
* @param {Array<Event>} add
* Items to add at the index.
* @returns {undefined}
* Nothing.
*/
addBefore(index, remove, add) {
addImplementation(this, index, remove, add, true)
}
/**
* Done, change the events.
*
* @param {Array<Event>} events
* List of events to apply the edits to.
* @returns {undefined}
* Nothing.
*/
consume(events) {
this.map.sort(function (a, b) {
return a[0] - b[0]
})
if (this.map.length === 0) {
return
}
// To do: if links are added in events, like they are in `markdown-rs`,
// this is needed.
// // Calculate jumps: where items in the current list move to.
// /** @type {Array<Jump>} */
// const jumps = []
// let index = 0
// let addAcc = 0
// let removeAcc = 0
// while (index < this.map.length) {
// const [at, remove, add] = this.map[index]
// removeAcc += remove
// addAcc += add.length
// jumps.push([at, removeAcc, addAcc])
// index += 1
// }
//
// shiftLinks(events, jumps)
let index = this.map.length
/** @type {Array<Array<Event>>} */
const vecs = []
while (index > 0) {
index -= 1
vecs.push(
events.slice(this.map[index][0] + this.map[index][1]),
this.map[index][2]
)
// Truncate rest.
events.length = this.map[index][0]
}
vecs.push(events.slice())
events.length = 0
let slice = vecs.pop()
while (slice) {
for (const element of slice) {
events.push(element)
}
slice = vecs.pop()
}
// Truncate everything.
this.map.length = 0
this.index.clear()
}
}
/**
* Create an edit.
*
* @param {EditMap} editMap
* Edit map to apply to.
* @param {number} at
* Index at which to apply the edit.
* @param {number} remove
* Count of items to remove at the index.
* @param {Array<Event>} add
* Items to add at the index.
* @param {boolean} before
* Insert `add` before existing additions at `at`, instead of after them.
* @returns {undefined}
* Nothing.
*/
function addImplementation(editMap, at, remove, add, before) {
if (remove === 0 && add.length === 0) {
return
}
const existing = editMap.index.get(at)
if (existing) {
existing[1] += remove
if (before) {
add.push(...existing[2])
existing[2] = add
} else {
existing[2].push(...add)
}
return
}
/** @type {Change} */
const change = [at, remove, add]
editMap.map.push(change)
editMap.index.set(at, change)
}
// /**
// * Shift `previous` and `next` links according to `jumps`.
// *
// * This fixes links in case there are events removed or added between them.
// *
// * @param {Array<Event>} events
// * @param {Array<Jump>} jumps
// */
// function shiftLinks(events, jumps) {
// let jumpIndex = 0
// let index = 0
// let add = 0
// let rm = 0
// while (index < events.length) {
// const rmCurr = rm
// while (jumpIndex < jumps.length && jumps[jumpIndex][0] <= index) {
// add = jumps[jumpIndex][2]
// rm = jumps[jumpIndex][1]
// jumpIndex += 1
// }
// // Ignore items that will be removed.
// if (rm > rmCurr) {
// index += rm - rmCurr
// } else {
// // ?
// // if let Some(link) = &events[index].link {
// // if let Some(next) = link.next {
// // events[next].link.as_mut().unwrap().previous = Some(index + add - rm);
// // while jumpIndex < jumps.len() && jumps[jumpIndex].0 <= next {
// // add = jumps[jumpIndex].2;
// // rm = jumps[jumpIndex].1;
// // jumpIndex += 1;
// // }
// // events[index].link.as_mut().unwrap().next = Some(next + add - rm);
// // index = next;
// // continue;
// // }
// // }
// index += 1
// }
// }
// }