@unified-latex/unified-latex-util-replace
Advanced tools
Comparing version 1.6.1 to 1.7.0
@@ -1,15 +0,79 @@ | ||
export * from "./libs/replace-node"; | ||
export * from "./libs/unified-latex-streaming-command"; | ||
export * from "./libs/replace-streaming-command"; | ||
export * from "./libs/replace-node-during-visit"; | ||
export * from "./libs/utils/significant-node"; | ||
import * as Ast from '@unified-latex/unified-latex-types'; | ||
import { Plugin as Plugin_2 } from 'unified'; | ||
import { VisitInfo } from '@unified-latex/unified-latex-util-visit'; | ||
/** | ||
* ## What is this? | ||
* Returns the first non-whitespace/non-comment node in `nodes`. If there is no such | ||
* node, `null` is returned. | ||
*/ | ||
export declare function firstSignificantNode(nodes: Ast.Node[], parbreaksAreInsignificant?: boolean): Ast.Node | null; | ||
/** | ||
* Returns the index of the first non-whitespace/non-comment node in `nodes`. If there is no such | ||
* node, `null` is returned. | ||
*/ | ||
export declare function firstSignificantNodeIndex(nodes: Ast.Node[], parbreaksAreInsignificant?: boolean): number | undefined; | ||
/** | ||
* Returns the last non-whitespace/non-comment node in `nodes`. If there is no such | ||
* node, `null` is returned. | ||
*/ | ||
export declare function lastSignificantNode(nodes: Ast.Node[], parbreaksAreInsignificant?: boolean): Ast.Node | null; | ||
/** | ||
* Returns the index of the last non-whitespace/non-comment node in `nodes`. If there is no such | ||
* node, `null` is returned. | ||
*/ | ||
export declare function lastSignificantNodeIndex(nodes: Ast.Node[], parbreaksAreInsignificant?: boolean): number | undefined; | ||
declare type PluginOptions = { | ||
replacers: Record<string, (content: Ast.Node[], streamingCommand: Ast.Macro) => Ast.Node | Ast.Node[]>; | ||
}; | ||
/** | ||
* Recursively replace nodes in `ast`. The `visitor` function is called on each node. If | ||
* `visitor` returns a node or an array of nodes, those nodes replace the node passed to `visitor`. | ||
* If `null` is returned, the node is deleted. If `undefined` is returned, no replacement happens. | ||
*/ | ||
export declare function replaceNode(ast: Ast.Ast, visitor: (node: Ast.Node | Ast.Argument, info: VisitInfo) => Ast.Node | Ast.Argument | (Ast.Node | Ast.Argument)[] | null | undefined | void): void; | ||
/** | ||
* Replaces the current node with `replacement`. It is assumed that the current | ||
* node is in an array that is a child of a parent element. If this is not the case, | ||
* the function will error. | ||
*/ | ||
export declare function replaceNodeDuringVisit(replacement: Ast.Node | Ast.Argument | (Ast.Node | Ast.Argument)[], info: VisitInfo): void; | ||
/** | ||
* Given a group or a node array, look for streaming commands (e.g., `\bfseries`) and replace them | ||
* with the specified macro. The "arguments" of the streaming command are passed to `replacer` and the return | ||
* value of `replacer` is inserted into the stream. | ||
* | ||
* Functions to help modify a `unified-latex` Abstract Syntax Tree (AST). | ||
* By default, this command will split at parbreaks (since commands like `\textbf{...} do not accept parbreaks in their | ||
* contents) and call `replacer` multiple times, once per paragraph. | ||
* | ||
* ## When should I use this? | ||
* Commands are also split at environments and at any macros listed in `macrosThatBreakPars`. | ||
*/ | ||
export declare function replaceStreamingCommand(ast: Ast.Group | Ast.Node[], isStreamingCommand: (node: any) => node is Ast.Macro, replacer: (content: Ast.Node[], streamingCommand: Ast.Macro) => Ast.Node | Ast.Node[], options?: { | ||
macrosThatBreakPars?: string[]; | ||
environmentsThatDontBreakPars?: string[]; | ||
}): Ast.Node[]; | ||
/** | ||
* Process streaming commands in a group. If needed, "escape" the group. | ||
* For example, `{\bfseries xx}` -> `\textbf{xx}`, but `{foo \bfseries xx}` -> `{foo \textbf{xx}}`. | ||
*/ | ||
export declare function replaceStreamingCommandInGroup(group: Ast.Group, isStreamingCommand: (node: any) => node is Ast.Macro, replacer: (content: Ast.Node[], streamingCommand: Ast.Macro) => Ast.Node | Ast.Node[], options?: { | ||
macrosThatBreakPars?: string[]; | ||
environmentsThatDontBreakPars?: string[]; | ||
}): Ast.Node[]; | ||
/** | ||
* Unified plugin to replace all found streaming commands with their argument-style equivalents. | ||
* This only applies to sections of the tree with no math ancestor. | ||
* | ||
* If you want to recursively replace particular AST nodes. | ||
* @param options.replacer A record of macro names and replacer functions. A replacer function accepts content and the original streaming command and is expected to return the argument-style command. It may be called multiple times per streaming command. | ||
*/ | ||
//# sourceMappingURL=index.d.ts.map | ||
export declare const unifiedLatexReplaceStreamingCommands: Plugin_2<PluginOptions[], Ast.Root, Ast.Root>; | ||
export { } |
94
index.js
@@ -1,3 +0,5 @@ | ||
// libs/replace-node.ts | ||
import { visit } from "@unified-latex/unified-latex-util-visit"; | ||
import { match } from "@unified-latex/unified-latex-util-match"; | ||
import { trimStart, trimEnd, trim } from "@unified-latex/unified-latex-util-trim"; | ||
import { splitOnCondition, unsplitOnMacro } from "@unified-latex/unified-latex-util-split"; | ||
function replaceNode(ast, visitor) { | ||
@@ -27,22 +29,2 @@ visit(ast, { | ||
} | ||
// libs/unified-latex-streaming-command.ts | ||
import { match as match5 } from "@unified-latex/unified-latex-util-match"; | ||
import { trimEnd as trimEnd3, trimStart as trimStart4 } from "@unified-latex/unified-latex-util-trim"; | ||
import { visit as visit2 } from "@unified-latex/unified-latex-util-visit"; | ||
// libs/replace-streaming-command.ts | ||
import { match as match4 } from "@unified-latex/unified-latex-util-match"; | ||
import { | ||
splitOnCondition, | ||
unsplitOnMacro | ||
} from "@unified-latex/unified-latex-util-split"; | ||
import { | ||
trim, | ||
trimEnd as trimEnd2, | ||
trimStart as trimStart3 | ||
} from "@unified-latex/unified-latex-util-trim"; | ||
// libs/utils/significant-node.ts | ||
import { match } from "@unified-latex/unified-latex-util-match"; | ||
function firstSignificantNode(nodes, parbreaksAreInsignificant) { | ||
@@ -82,11 +64,4 @@ const index = firstSignificantNodeIndex(nodes, parbreaksAreInsignificant); | ||
} | ||
// libs/utils/replace-streaming-command-in-array.ts | ||
import { trimEnd, trimStart as trimStart2 } from "@unified-latex/unified-latex-util-trim"; | ||
// libs/utils/join-without-excess-whitespace.ts | ||
import { match as match2 } from "@unified-latex/unified-latex-util-match"; | ||
import { trimStart } from "@unified-latex/unified-latex-util-trim"; | ||
function isSpaceLike(node) { | ||
return match2.whitespace(node) || match2.comment(node) && Boolean(node.leadingWhitespace); | ||
return match.whitespace(node) || match.comment(node) && Boolean(node.leadingWhitespace); | ||
} | ||
@@ -103,3 +78,3 @@ function joinWithoutExcessWhitespace(head, tail) { | ||
const tailStart = tail[0]; | ||
if (match2.whitespace(headEnd) && match2.whitespace(tailStart)) { | ||
if (match.whitespace(headEnd) && match.whitespace(tailStart)) { | ||
head.push(...tail.slice(1)); | ||
@@ -109,3 +84,3 @@ return; | ||
if (!isSpaceLike(headEnd) || !isSpaceLike(tailStart)) { | ||
if (match2.whitespace(headEnd) && match2.comment(tailStart)) { | ||
if (match.whitespace(headEnd) && match.comment(tailStart)) { | ||
const comment2 = { | ||
@@ -126,3 +101,3 @@ type: "comment", | ||
} | ||
if (match2.comment(headEnd) && match2.comment(tailStart)) { | ||
if (match.comment(headEnd) && match.comment(tailStart)) { | ||
if (tailStart.leadingWhitespace || tailStart.sameline) { | ||
@@ -138,4 +113,4 @@ head.push( | ||
} | ||
let comment = match2.comment(headEnd) ? headEnd : tailStart; | ||
if (!match2.comment(comment)) { | ||
let comment = match.comment(headEnd) ? headEnd : tailStart; | ||
if (!match.comment(comment)) { | ||
throw new Error( | ||
@@ -156,5 +131,2 @@ `Expected a comment but found ${JSON.stringify(comment)}` | ||
} | ||
// libs/utils/wrap-significant-content.ts | ||
import { match as match3 } from "@unified-latex/unified-latex-util-match"; | ||
function wrapSignificantContent(content, wrapper) { | ||
@@ -164,3 +136,3 @@ let hoistUntil = 0; | ||
for (let i = 0; i < content.length; i++) { | ||
if (match3.whitespace(content[i]) || match3.comment(content[i])) { | ||
if (match.whitespace(content[i]) || match.comment(content[i])) { | ||
hoistUntil = i + 1; | ||
@@ -172,3 +144,3 @@ continue; | ||
for (let j = content.length - 1; j >= 0; j--) { | ||
if (match3.whitespace(content[j]) || match3.comment(content[j])) { | ||
if (match.whitespace(content[j]) || match.comment(content[j])) { | ||
hoistAfter = j; | ||
@@ -193,4 +165,2 @@ continue; | ||
} | ||
// libs/utils/replace-streaming-command-in-array.ts | ||
function replaceStreamingCommandInArray(nodes, isStreamingCommand, replacer) { | ||
@@ -207,3 +177,3 @@ while (nodes.length > 0 && isStreamingCommand(nodes[nodes.length - 1])) { | ||
let tail = nodes.slice(i + 1); | ||
trimStart2(tail); | ||
trimStart(tail); | ||
tail = wrapSignificantContent(tail, wrapper); | ||
@@ -217,4 +187,2 @@ foundStreamingCommands.push(node); | ||
} | ||
// libs/replace-streaming-command.ts | ||
function replaceStreamingCommandInGroup(group, isStreamingCommand, replacer, options) { | ||
@@ -260,3 +228,3 @@ const content = group.content; | ||
let processedContent = []; | ||
if (match4.group(ast)) { | ||
if (match.group(ast)) { | ||
processedContent = replaceStreamingCommandInGroup( | ||
@@ -272,3 +240,3 @@ ast, | ||
let sliceIndex = scanIndex; | ||
while (scanIndex > 0 && (isStreamingCommand(nodes[scanIndex - 1]) || match4.whitespace(nodes[scanIndex - 1]))) { | ||
while (scanIndex > 0 && (isStreamingCommand(nodes[scanIndex - 1]) || match.whitespace(nodes[scanIndex - 1]))) { | ||
scanIndex--; | ||
@@ -282,10 +250,10 @@ if (isStreamingCommand(nodes[scanIndex])) { | ||
} | ||
const macroThatBreaks = match4.createMacroMatcher(macrosThatBreakPars); | ||
const envThatDoesntBreak = match4.createEnvironmentMatcher( | ||
const macroThatBreaks = match.createMacroMatcher(macrosThatBreakPars); | ||
const envThatDoesntBreak = match.createEnvironmentMatcher( | ||
environmentsThatDontBreakPars | ||
); | ||
const isPar = (node) => match4.parbreak(node) || match4.macro(node, "par") || macroThatBreaks(node) || match4.environment(node) && !envThatDoesntBreak(node) || node.type === "displaymath"; | ||
const isPar = (node) => match.parbreak(node) || match.macro(node, "par") || macroThatBreaks(node) || match.environment(node) && !envThatDoesntBreak(node) || node.type === "displaymath"; | ||
const splitByPar = splitOnCondition(nodes, isPar); | ||
splitByPar.separators = splitByPar.separators.map( | ||
(sep) => match4.macro(sep, "par") ? { type: "parbreak" } : sep | ||
(sep) => match.macro(sep, "par") ? { type: "parbreak" } : sep | ||
); | ||
@@ -326,5 +294,5 @@ const replacers = []; | ||
if (i === 0) { | ||
trimEnd2(segment); | ||
trimEnd(segment); | ||
} else if (i === segments.length - 1) { | ||
trimStart3(segment); | ||
trimStart(segment); | ||
} else { | ||
@@ -355,4 +323,2 @@ trim(segment); | ||
} | ||
// libs/replace-node-during-visit.ts | ||
function replaceNodeDuringVisit(replacement, info) { | ||
@@ -376,9 +342,7 @@ const parent = info.parents[0]; | ||
} | ||
// libs/unified-latex-streaming-command.ts | ||
var unifiedLatexReplaceStreamingCommands = function unifiedLatexReplaceStreamingCommands2(options) { | ||
const unifiedLatexReplaceStreamingCommands = function unifiedLatexReplaceStreamingCommands2(options) { | ||
const { replacers = {} } = options || {}; | ||
const isReplaceable = match5.createMacroMatcher(replacers); | ||
const isReplaceable = match.createMacroMatcher(replacers); | ||
return (tree) => { | ||
visit2( | ||
visit( | ||
tree, | ||
@@ -401,13 +365,13 @@ (group, info) => { | ||
const nextToken = info.containingArray[info.index + 1]; | ||
if (match5.whitespaceLike(prevToken) && match5.whitespaceLike(fixed[0])) { | ||
trimStart4(fixed); | ||
if (match.whitespaceLike(prevToken) && match.whitespaceLike(fixed[0])) { | ||
trimStart(fixed); | ||
} | ||
if (match5.whitespaceLike(nextToken) && match5.whitespaceLike(fixed[fixed.length - 1])) { | ||
trimEnd3(fixed); | ||
if (match.whitespaceLike(nextToken) && match.whitespaceLike(fixed[fixed.length - 1])) { | ||
trimEnd(fixed); | ||
} | ||
replaceNodeDuringVisit(fixed, info); | ||
}, | ||
{ test: match5.group } | ||
{ test: match.group } | ||
); | ||
visit2( | ||
visit( | ||
tree, | ||
@@ -414,0 +378,0 @@ (nodes, info) => { |
{ | ||
"name": "@unified-latex/unified-latex-util-replace", | ||
"version": "1.6.1", | ||
"version": "1.7.0", | ||
"description": "Functions for modifying a unified-latex AST", | ||
@@ -8,7 +8,7 @@ "main": "index.js", | ||
"dependencies": { | ||
"@unified-latex/unified-latex-types": "^1.6.1", | ||
"@unified-latex/unified-latex-util-match": "^1.6.1", | ||
"@unified-latex/unified-latex-util-split": "^1.6.1", | ||
"@unified-latex/unified-latex-util-trim": "^1.6.1", | ||
"@unified-latex/unified-latex-util-visit": "^1.6.1", | ||
"@unified-latex/unified-latex-types": "^1.7.0", | ||
"@unified-latex/unified-latex-util-match": "^1.7.0", | ||
"@unified-latex/unified-latex-util-split": "^1.7.0", | ||
"@unified-latex/unified-latex-util-trim": "^1.7.0", | ||
"@unified-latex/unified-latex-util-visit": "^1.7.0", | ||
"unified": "^10.1.2" | ||
@@ -15,0 +15,0 @@ }, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
104302
7
848