Files
Loki/frontend/node_modules/mdast-util-find-and-replace/lib/index.js
T
MichaelandClaude Opus 5 b405adea6c Jauge de contexte, espace disque sur partage agrégé, panneau Fichiers
Trois défauts sans rapport entre eux, tous visibles à l'écran.

Jauge de contexte bloquée à « 0 / N (0 %) » : le comptage exact vient de
usage.prompt_tokens (include_usage), qu'un llama-server récent a cessé de
renvoyer. On note désormais si l'usage est arrivé ; sinon la fin de tour
publie l'estimation qui pilote déjà la compaction — approximative, mais
jamais absente. Et la souscription envoie la valeur courante juste après
le rattrapage : une page rechargée affichait zéro sur une discussion
pourtant pleine, faute d'événement ctx_used dans la fenêtre rejouée.

Espace disque : « il ne reste que 11,9 Go » sur un partage Unraid qui en
a des centaines. /mnt/user est servi par shfs, un FUSE qui agrège
plusieurs disques — statfs y renvoie l'espace d'un seul, et ce chiffre
REFUSAIT l'installation du modèle. Les systèmes de fichiers qui
approximent (FUSE, overlay, NFS, CIFS, Ceph, Gluster) sont reconnus par
leur magie : leur chiffre reste affiché, précédé d'un « ~ », mais ne
bloque plus rien.

Panneau Fichiers : le panneau entier défilait, emportant vers le haut
l'en-tête, le fil d'Ariane et le pied (occupation disque, bouton .zip)
dès qu'il y avait quelques fichiers. Seule la liste défile maintenant ;
le pied reste en bas et l'état vide se centre.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-19 08:28:53 +02:00

262 lines
6.5 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 {Nodes, Parents, PhrasingContent, Root, Text} from 'mdast'
* @import {BuildVisitor, Test, VisitorResult} from 'unist-util-visit-parents'
*/
/**
* @typedef RegExpMatchObject
* Info on the match.
* @property {number} index
* The index of the search at which the result was found.
* @property {string} input
* A copy of the search string in the text node.
* @property {[...Array<Parents>, Text]} stack
* All ancestors of the text node, where the last node is the text itself.
*
* @typedef {RegExp | string} Find
* Pattern to find.
*
* Strings are escaped and then turned into global expressions.
*
* @typedef {Array<FindAndReplaceTuple>} FindAndReplaceList
* Several find and replaces, in array form.
*
* @typedef {[Find, Replace?]} FindAndReplaceTuple
* Find and replace in tuple form.
*
* @typedef {ReplaceFunction | string | null | undefined} Replace
* Thing to replace with.
*
* @callback ReplaceFunction
* Callback called when a search matches.
* @param {...any} parameters
* The parameters are the result of corresponding search expression:
*
* * `value` (`string`) — whole match
* * `...capture` (`Array<string>`) — matches from regex capture groups
* * `match` (`RegExpMatchObject`) — info on the match
* @returns {Array<PhrasingContent> | PhrasingContent | string | false | null | undefined}
* Thing to replace with.
*
* * when `null`, `undefined`, `''`, remove the match
* * …or when `false`, do not replace at all
* * …or when `string`, replace with a text node of that value
* * …or when `Node` or `Array<Node>`, replace with those nodes
*
* @typedef {[RegExp, ReplaceFunction]} Pair
* Normalized find and replace.
*
* @typedef {Array<Pair>} Pairs
* All find and replaced.
*
* @typedef Options
* Configuration.
* @property {Test | null | undefined} [ignore]
* Test for which nodes to ignore (optional).
*/
import escape from 'escape-string-regexp'
import {visitParents} from 'unist-util-visit-parents'
import {convert} from 'unist-util-is'
/**
* Find patterns in a tree and replace them.
*
* The algorithm searches the tree in *preorder* for complete values in `Text`
* nodes.
* Partial matches are not supported.
*
* @param {Nodes} tree
* Tree to change.
* @param {FindAndReplaceList | FindAndReplaceTuple} list
* Patterns to find.
* @param {Options | null | undefined} [options]
* Configuration (when `find` is not `Find`).
* @returns {undefined}
* Nothing.
*/
export function findAndReplace(tree, list, options) {
const settings = options || {}
const ignored = convert(settings.ignore || [])
const pairs = toPairs(list)
let pairIndex = -1
while (++pairIndex < pairs.length) {
visitParents(tree, 'text', visitor)
}
/** @type {BuildVisitor<Root, 'text'>} */
function visitor(node, parents) {
let index = -1
/** @type {Parents | undefined} */
let grandparent
while (++index < parents.length) {
const parent = parents[index]
/** @type {Array<Nodes> | undefined} */
const siblings = grandparent ? grandparent.children : undefined
if (
ignored(
parent,
siblings ? siblings.indexOf(parent) : undefined,
grandparent
)
) {
return
}
grandparent = parent
}
if (grandparent) {
return handler(node, parents)
}
}
/**
* Handle a text node which is not in an ignored parent.
*
* @param {Text} node
* Text node.
* @param {Array<Parents>} parents
* Parents.
* @returns {VisitorResult}
* Result.
*/
function handler(node, parents) {
const parent = parents[parents.length - 1]
const find = pairs[pairIndex][0]
const replace = pairs[pairIndex][1]
let start = 0
/** @type {Array<Nodes>} */
const siblings = parent.children
const index = siblings.indexOf(node)
let change = false
/** @type {Array<PhrasingContent>} */
let nodes = []
find.lastIndex = 0
let match = find.exec(node.value)
while (match) {
const position = match.index
/** @type {RegExpMatchObject} */
const matchObject = {
index: match.index,
input: match.input,
stack: [...parents, node]
}
let value = replace(...match, matchObject)
if (typeof value === 'string') {
value = value.length > 0 ? {type: 'text', value} : undefined
}
// It wasn’t a match after all.
if (value === false) {
// False acts as if there was no match.
// So we need to reset `lastIndex`, which currently being at the end of
// the current match, to the beginning.
find.lastIndex = position + 1
} else {
if (start !== position) {
nodes.push({
type: 'text',
value: node.value.slice(start, position)
})
}
if (Array.isArray(value)) {
nodes.push(...value)
} else if (value) {
nodes.push(value)
}
start = position + match[0].length
change = true
}
if (!find.global) {
break
}
match = find.exec(node.value)
}
if (change) {
if (start < node.value.length) {
nodes.push({type: 'text', value: node.value.slice(start)})
}
parent.children.splice(index, 1, ...nodes)
} else {
nodes = [node]
}
return index + nodes.length
}
}
/**
* Turn a tuple or a list of tuples into pairs.
*
* @param {FindAndReplaceList | FindAndReplaceTuple} tupleOrList
* Schema.
* @returns {Pairs}
* Clean pairs.
*/
function toPairs(tupleOrList) {
/** @type {Pairs} */
const result = []
if (!Array.isArray(tupleOrList)) {
throw new TypeError('Expected find and replace tuple or list of tuples')
}
/** @type {FindAndReplaceList} */
// @ts-expect-error: correct.
const list =
!tupleOrList[0] || Array.isArray(tupleOrList[0])
? tupleOrList
: [tupleOrList]
let index = -1
while (++index < list.length) {
const tuple = list[index]
result.push([toExpression(tuple[0]), toFunction(tuple[1])])
}
return result
}
/**
* Turn a find into an expression.
*
* @param {Find} find
* Find.
* @returns {RegExp}
* Expression.
*/
function toExpression(find) {
return typeof find === 'string' ? new RegExp(escape(find), 'g') : find
}
/**
* Turn a replace into a function.
*
* @param {Replace} replace
* Replace.
* @returns {ReplaceFunction}
* Function.
*/
function toFunction(replace) {
return typeof replace === 'function'
? replace
: function () {
return replace
}
}