myst-common
Advanced tools
Comparing version 1.7.1 to 1.7.2
import type { Block } from 'myst-spec-ext'; | ||
import type { GenericParent } from './types.js'; | ||
import type { FrontmatterParts, GenericParent } from './types.js'; | ||
/** | ||
@@ -9,2 +9,11 @@ * Selects the block node(s) based on part (string) or tags (string[]). | ||
/** | ||
* Selects the frontmatterParts entries by `part` | ||
* | ||
* If `part` is a string array, any matching part from the frontmatter will be | ||
* returned. | ||
* | ||
* Returns array of blocks. | ||
*/ | ||
export declare function selectFrontmatterParts(frontmatterParts?: FrontmatterParts, part?: string | string[]): Block[]; | ||
/** | ||
* Extract implicit part based on heading name | ||
@@ -23,3 +32,5 @@ * | ||
/** | ||
* Returns a copy of the block parts and removes them from the tree. | ||
* Returns a copy of block parts, if defined in the tree, and removes them from the tree. | ||
* | ||
* This does not look at parts defined in frontmatter. | ||
*/ | ||
@@ -33,3 +44,5 @@ export declare function extractPart(tree: GenericParent, part?: string | string[], opts?: { | ||
requireExplicitPart?: boolean; | ||
/** Dictionary of part trees, processed from frontmatter */ | ||
frontmatterParts?: FrontmatterParts; | ||
}): GenericParent | undefined; | ||
//# sourceMappingURL=extractParts.d.ts.map |
@@ -46,2 +46,25 @@ import { remove } from 'unist-util-remove'; | ||
} | ||
/** | ||
* Selects the frontmatterParts entries by `part` | ||
* | ||
* If `part` is a string array, any matching part from the frontmatter will be | ||
* returned. | ||
* | ||
* Returns array of blocks. | ||
*/ | ||
export function selectFrontmatterParts(frontmatterParts, part) { | ||
if (!frontmatterParts) | ||
return []; | ||
const parts = coercePart(part); | ||
if (parts.length === 0) | ||
return []; | ||
const blockParts = []; | ||
parts.forEach((p) => { | ||
Object.entries(frontmatterParts).forEach(([key, value]) => { | ||
if (p === key.toLowerCase()) | ||
blockParts.push(...value.mdast.children); | ||
}); | ||
}); | ||
return blockParts; | ||
} | ||
function createPartBlock(children, part, opts) { | ||
@@ -127,3 +150,5 @@ var _a; | ||
/** | ||
* Returns a copy of the block parts and removes them from the tree. | ||
* Returns a copy of block parts, if defined in the tree, and removes them from the tree. | ||
* | ||
* This does not look at parts defined in frontmatter. | ||
*/ | ||
@@ -134,4 +159,5 @@ export function extractPart(tree, part, opts) { | ||
return; | ||
const frontmatterParts = selectFrontmatterParts(opts === null || opts === void 0 ? void 0 : opts.frontmatterParts, part); | ||
const blockParts = selectBlockParts(tree, part); | ||
if (blockParts.length === 0) { | ||
if (frontmatterParts.length === 0 && blockParts.length === 0) { | ||
if (opts === null || opts === void 0 ? void 0 : opts.requireExplicitPart) | ||
@@ -141,3 +167,3 @@ return; | ||
} | ||
const children = copyNode(blockParts).map((block) => { | ||
const children = copyNode(frontmatterParts.length > 0 ? frontmatterParts : blockParts).map((block) => { | ||
var _a; | ||
@@ -163,3 +189,3 @@ // Ensure the block always has the `part` defined, as it might be in the tags | ||
const partsTree = { type: 'root', children }; | ||
// Remove the block parts from the main document | ||
// Remove the block parts from the main document, even if frontmatter parts are returned | ||
blockParts.forEach((block) => { | ||
@@ -166,0 +192,0 @@ block.type = '__delete__'; |
@@ -11,3 +11,3 @@ export { admonitionKindToTitle, toText, fileError, fileWarn, fileInfo, createId, normalizeLabel, createHtmlId, transferTargetAttrs, liftChildren, setTextAsChild, copyNode, mergeTextNodes, writeTexLabelledComment, getMetadataTags, } from './utils.js'; | ||
export type { MessageInfo } from './utils.js'; | ||
export type { IExpressionResult, IExpressionError, IExpressionOutput, GenericNode, GenericParent, Citations, References, ArgDefinition, BodyDefinition, OptionDefinition, DirectiveData, RoleData, DirectiveSpec, DirectiveContext, RoleSpec, ParseTypes, MystPlugin, ValidatedMystPlugin, PluginOptions, PluginUtils, TransformSpec, } from './types.js'; | ||
export type { IExpressionResult, IExpressionError, IExpressionOutput, GenericNode, GenericParent, Citations, References, ArgDefinition, BodyDefinition, OptionDefinition, DirectiveData, RoleData, DirectiveSpec, DirectiveContext, RoleSpec, ParseTypes, MystPlugin, ValidatedMystPlugin, PluginOptions, PluginUtils, TransformSpec, FrontmatterPart, FrontmatterParts, } from './types.js'; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -6,2 +6,3 @@ import type { Plugin } from 'unified'; | ||
import type { PartialJSONObject } from '@lumino/coreutils'; | ||
import type { PageFrontmatter } from 'myst-frontmatter'; | ||
export type GenericNode<T extends Record<string, any> = Record<string, any>> = { | ||
@@ -169,3 +170,8 @@ type: string; | ||
export type IExpressionResult = IExpressionError | IExpressionOutput; | ||
export type FrontmatterPart = { | ||
mdast: GenericParent; | ||
frontmatter?: PageFrontmatter; | ||
}; | ||
export type FrontmatterParts = Record<string, FrontmatterPart>; | ||
export {}; | ||
//# sourceMappingURL=types.d.ts.map |
{ | ||
"name": "myst-common", | ||
"sideEffects": false, | ||
"version": "1.7.1", | ||
"version": "1.7.2", | ||
"type": "module", | ||
@@ -23,3 +23,3 @@ "exports": "./dist/index.js", | ||
"mdast": "^3.0.0", | ||
"myst-frontmatter": "^1.7.1", | ||
"myst-frontmatter": "^1.7.2", | ||
"myst-spec": "^0.0.5", | ||
@@ -35,3 +35,3 @@ "nanoid": "^4.0.0", | ||
"devDependencies": { | ||
"myst-spec-ext": "^1.7.1", | ||
"myst-spec-ext": "^1.7.2", | ||
"unist-builder": "3.0.0", | ||
@@ -38,0 +38,0 @@ "@jupyterlab/nbformat": "^3.5.2", |
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
66023
1290
Updatedmyst-frontmatter@^1.7.2