myst-transforms
Advanced tools
Comparing version 1.1.0 to 1.1.1
import type { Plugin } from 'unified'; | ||
import type { GenericParent } from 'myst-common'; | ||
import { AdmonitionKind } from './types.js'; | ||
type Options = { | ||
@@ -8,3 +7,2 @@ /** Replace the admonition title with the first paragraph if it is all bold. */ | ||
}; | ||
export declare function admonitionKindToTitle(kind: AdmonitionKind | string): string; | ||
/** | ||
@@ -11,0 +9,0 @@ * Visit all admonitions and add headers if necessary |
import { selectAll } from 'unist-util-select'; | ||
import { AdmonitionKind } from './types.js'; | ||
import { AdmonitionKind, admonitionKindToTitle } from 'myst-common'; | ||
const githubAdmonitionKinds = ['note', 'important', 'warning']; | ||
export function admonitionKindToTitle(kind) { | ||
const transform = { | ||
attention: 'Attention', | ||
caution: 'Caution', | ||
danger: 'Danger', | ||
error: 'Error', | ||
important: 'Important', | ||
hint: 'Hint', | ||
note: 'Note', | ||
seealso: 'See Also', | ||
tip: 'Tip', | ||
warning: 'Warning', | ||
}; | ||
return transform[kind] || `Unknown Admonition "${kind}"`; | ||
} | ||
/** | ||
@@ -20,0 +5,0 @@ * Visit all admonitions and add headers if necessary |
@@ -5,2 +5,3 @@ import type { Plugin } from 'unified'; | ||
import type { GenericParent } from 'myst-common'; | ||
import { TargetKind } from 'myst-common'; | ||
type ResolvableCrossReference = Omit<CrossReference, 'kind'> & { | ||
@@ -16,9 +17,2 @@ kind?: TargetKind | string; | ||
}; | ||
export declare enum TargetKind { | ||
heading = "heading", | ||
equation = "equation", | ||
figure = "figure", | ||
table = "table", | ||
code = "code" | ||
} | ||
export declare enum ReferenceKind { | ||
@@ -25,0 +19,0 @@ ref = "ref", |
import { visit } from 'unist-util-visit'; | ||
import { select, selectAll } from 'unist-util-select'; | ||
import { findAndReplace } from 'mdast-util-find-and-replace'; | ||
import { createHtmlId, fileWarn, normalizeLabel, setTextAsChild, copyNode, liftChildren, } from 'myst-common'; | ||
import { createHtmlId, fileWarn, normalizeLabel, setTextAsChild, copyNode, liftChildren, TargetKind, } from 'myst-common'; | ||
const TRANSFORM_NAME = 'myst-transforms:enumerate'; | ||
export var TargetKind; | ||
(function (TargetKind) { | ||
TargetKind["heading"] = "heading"; | ||
TargetKind["equation"] = "equation"; | ||
TargetKind["figure"] = "figure"; | ||
TargetKind["table"] = "table"; | ||
TargetKind["code"] = "code"; | ||
})(TargetKind || (TargetKind = {})); | ||
function getDefaultNumberedReferenceLabel(kind) { | ||
@@ -15,0 +7,0 @@ switch (kind) { |
import type { Plugin } from 'unified'; | ||
import type { GenericParent } from 'myst-common'; | ||
import type { Handle } from 'hast-util-to-mdast'; | ||
import type { GenericParent } from 'myst-common'; | ||
export type HtmlTransformOptions = { | ||
@@ -11,3 +11,10 @@ keepBreaks?: boolean; | ||
export declare function htmlTransform(tree: GenericParent, opts?: HtmlTransformOptions): GenericParent; | ||
/** | ||
* Traverse mdast tree to reconstruct html elements split across mdast nodes into a single node | ||
* | ||
* This function identifies html "opening" nodes, then collects the subsequent mdast nodes until | ||
* it encounters a "closing" node, when it consolidates all the nodes into a single html node. | ||
*/ | ||
export declare function reconstructHtmlTransform(tree: GenericParent): GenericParent; | ||
export declare const htmlPlugin: Plugin<[HtmlTransformOptions?], GenericParent, GenericParent>; | ||
//# sourceMappingURL=html.d.ts.map |
import { unified } from 'unified'; | ||
import { liftChildren } from 'myst-common'; | ||
import { mystToHtml } from 'myst-to-html'; | ||
import { fromHtml } from 'hast-util-from-html'; | ||
import { all } from 'hast-util-to-mdast'; | ||
import { remove } from 'unist-util-remove'; | ||
import { selectAll } from 'unist-util-select'; | ||
@@ -7,3 +11,2 @@ import { visit } from 'unist-util-visit'; | ||
import rehypeRemark from 'rehype-remark'; | ||
import { liftChildren } from 'myst-common'; | ||
const defaultHtmlToMdastOptions = { | ||
@@ -60,4 +63,81 @@ keepBreaks: true, | ||
} | ||
/** | ||
* Convert html nodes and children to single html node | ||
* | ||
* This function takes the html nodes with opening and closing tags; all the mdast content | ||
* between these nodes is present as 'children' on the opening node. The mdast content is | ||
* then converted to html, wrapped by the opening and closing tag, and saved to a single html | ||
* node. All of the processed nodes are then marked for deletion. | ||
*/ | ||
function finalizeNode(htmlOpenNodeWithChildren, htmlCloseNode) { | ||
var _a, _b; | ||
const innerHtml = mystToHtml({ type: 'root', children: htmlOpenNodeWithChildren.children }, { | ||
hast: { | ||
handlers: { | ||
html: (h, node) => { | ||
return fromHtml(node.value, { fragment: true }).children; | ||
}, | ||
}, | ||
}, | ||
}); | ||
// This would be good to sanitize, but the best solution requires jsdom, increasing build size by 50%... | ||
htmlOpenNodeWithChildren.value = `${(_a = htmlOpenNodeWithChildren.value) === null || _a === void 0 ? void 0 : _a.trim()}${innerHtml}${(_b = htmlCloseNode.value) === null || _b === void 0 ? void 0 : _b.trim()}`; | ||
htmlOpenNodeWithChildren.children.forEach((child) => { | ||
child.type = '__delete__'; | ||
}); | ||
htmlCloseNode.type = '__delete__'; | ||
delete htmlOpenNodeWithChildren.children; | ||
} | ||
function reconstructHtml(tree) { | ||
const htmlOpenNodes = []; | ||
tree.children.forEach((child) => { | ||
var _a; | ||
if (child.type === 'html') { | ||
const value = (_a = child.value) === null || _a === void 0 ? void 0 : _a.trim(); | ||
if (value === null || value === void 0 ? void 0 : value.startsWith('</')) { | ||
// In this case, child is a standalone closing html node | ||
const htmlOpenNode = htmlOpenNodes.pop(); | ||
if (!htmlOpenNode) { | ||
return; | ||
} | ||
finalizeNode(htmlOpenNode, child); | ||
if (htmlOpenNodes.length) { | ||
htmlOpenNodes[htmlOpenNodes.length - 1].children.push(htmlOpenNode); | ||
} | ||
} | ||
else if (!(value === null || value === void 0 ? void 0 : value.endsWith('/>')) && !(value === null || value === void 0 ? void 0 : value.endsWith('-->'))) { | ||
// In this case, child is a standalone opening html node | ||
child.children = []; | ||
htmlOpenNodes.push(child); | ||
} | ||
} | ||
else { | ||
if (child.children) { | ||
// Recursively process children | ||
reconstructHtml(child); | ||
} | ||
if (htmlOpenNodes.length) { | ||
// If we are between an opening and closing node, add this to the html content to be processed | ||
htmlOpenNodes[htmlOpenNodes.length - 1].children.push(child); | ||
} | ||
} | ||
}); | ||
// At this point, any htmlOpenNodes are errors; just clean them up. | ||
htmlOpenNodes.forEach((node) => { | ||
delete node.children; | ||
}); | ||
} | ||
/** | ||
* Traverse mdast tree to reconstruct html elements split across mdast nodes into a single node | ||
* | ||
* This function identifies html "opening" nodes, then collects the subsequent mdast nodes until | ||
* it encounters a "closing" node, when it consolidates all the nodes into a single html node. | ||
*/ | ||
export function reconstructHtmlTransform(tree) { | ||
reconstructHtml(tree); | ||
remove(tree, '__delete__'); | ||
return tree; | ||
} | ||
export const htmlPlugin = (opts) => (tree) => { | ||
htmlTransform(tree, opts); | ||
}; |
@@ -1,6 +0,5 @@ | ||
export { admonitionHeadersPlugin, admonitionHeadersTransform, admonitionBlockquotePlugin, admonitionBlockquoteTransform, admonitionKindToTitle, } from './admonitions.js'; | ||
export { AdmonitionKind } from './types.js'; | ||
export { admonitionHeadersPlugin, admonitionHeadersTransform, admonitionBlockquotePlugin, admonitionBlockquoteTransform, } from './admonitions.js'; | ||
export { captionParagraphPlugin, captionParagraphTransform } from './caption.js'; | ||
export { footnotesPlugin, footnotesTransform } from './footnotes.js'; | ||
export { htmlPlugin, htmlTransform } from './html.js'; | ||
export { htmlPlugin, htmlTransform, reconstructHtmlTransform } from './html.js'; | ||
export { htmlIdsPlugin, htmlIdsTransform } from './htmlIds.js'; | ||
@@ -19,3 +18,3 @@ export { keysPlugin, keysTransform } from './keys.js'; | ||
export { abbreviationPlugin, abbreviationTransform } from './abbreviations.js'; | ||
export type { IReferenceState, NumberingOptions, TargetKind, ReferenceKind } from './enumerate.js'; | ||
export type { IReferenceState, NumberingOptions, ReferenceKind } from './enumerate.js'; | ||
export { enumerateTargetsTransform, enumerateTargetsPlugin, resolveReferencesTransform, resolveReferencesPlugin, ReferenceState, MultiPageReferenceState, } from './enumerate.js'; | ||
@@ -22,0 +21,0 @@ export { basicTransformationsPlugin, basicTransformations } from './basic.js'; |
@@ -1,6 +0,5 @@ | ||
export { admonitionHeadersPlugin, admonitionHeadersTransform, admonitionBlockquotePlugin, admonitionBlockquoteTransform, admonitionKindToTitle, } from './admonitions.js'; | ||
export { AdmonitionKind } from './types.js'; | ||
export { admonitionHeadersPlugin, admonitionHeadersTransform, admonitionBlockquotePlugin, admonitionBlockquoteTransform, } from './admonitions.js'; | ||
export { captionParagraphPlugin, captionParagraphTransform } from './caption.js'; | ||
export { footnotesPlugin, footnotesTransform } from './footnotes.js'; | ||
export { htmlPlugin, htmlTransform } from './html.js'; | ||
export { htmlPlugin, htmlTransform, reconstructHtmlTransform } from './html.js'; | ||
export { htmlIdsPlugin, htmlIdsTransform } from './htmlIds.js'; | ||
@@ -7,0 +6,0 @@ export { keysPlugin, keysTransform } from './keys.js'; |
{ | ||
"name": "myst-transforms", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"sideEffects": false, | ||
@@ -23,2 +23,3 @@ "type": "module", | ||
"doi-utils": "^2.0.0", | ||
"hast-util-from-html": "^2.0.1", | ||
"intersphinx": "^1.0.2", | ||
@@ -28,5 +29,6 @@ "js-yaml": "^4.1.0", | ||
"mdast-util-find-and-replace": "^2.1.0", | ||
"myst-common": "^1.1.2", | ||
"myst-common": "^1.1.5", | ||
"myst-spec": "^0.0.4", | ||
"myst-spec-ext": "^1.1.2", | ||
"myst-spec-ext": "^1.1.5", | ||
"myst-to-html": "1.0.7", | ||
"rehype-parse": "^8.0.4", | ||
@@ -33,0 +35,0 @@ "rehype-remark": "^9.1.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
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
121685
2462
0
20
92
+ Addedhast-util-from-html@^2.0.1
+ Addedmyst-to-html@1.0.7
+ Added@types/hast@3.0.4(transitive)
+ Added@types/parse5@6.0.3(transitive)
+ Added@types/unist@3.0.3(transitive)
+ Addedccount@2.0.1(transitive)
+ Addedcharacter-entities-html4@2.1.0(transitive)
+ Addedcharacter-entities-legacy@3.0.0(transitive)
+ Addedclassnames@2.5.1(transitive)
+ Addeddequal@2.0.3(transitive)
+ Addeddevlop@1.1.0(transitive)
+ Addedentities@4.5.0(transitive)
+ Addedhast@1.0.0(transitive)
+ Addedhast-util-from-html@2.0.3(transitive)
+ Addedhast-util-from-parse5@8.0.3(transitive)
+ Addedhast-util-parse-selector@4.0.0(transitive)
+ Addedhast-util-raw@7.2.3(transitive)
+ Addedhast-util-to-html@8.0.4(transitive)
+ Addedhast-util-to-parse5@7.1.0(transitive)
+ Addedhastscript@9.0.1(transitive)
+ Addedhtml-void-elements@2.0.1(transitive)
+ Addedhtml-whitespace-sensitive-tag-names@2.0.0(transitive)
+ Addedmdast-util-definitions@5.1.2(transitive)
+ Addedmdast-util-to-hast@12.3.0(transitive)
+ Addedmicromark-util-character@1.2.0(transitive)
+ Addedmicromark-util-encode@1.1.0(transitive)
+ Addedmicromark-util-sanitize-uri@1.2.0(transitive)
+ Addedmicromark-util-symbol@1.1.0(transitive)
+ Addedmicromark-util-types@1.1.0(transitive)
+ Addedmyst-to-html@1.0.7(transitive)
+ Addedparse5@7.2.1(transitive)
+ Addedproperty-information@7.0.0(transitive)
+ Addedrehype-format@4.0.1(transitive)
+ Addedrehype-stringify@9.0.4(transitive)
+ Addedstringify-entities@4.0.4(transitive)
+ Addedtrim-lines@3.0.1(transitive)
+ Addedunist-util-generated@2.0.1(transitive)
+ Addedunist-util-position@4.0.4(transitive)
+ Addedunist-util-stringify-position@4.0.0(transitive)
+ Addedvfile@6.0.3(transitive)
+ Addedvfile-location@5.0.3(transitive)
+ Addedvfile-message@4.0.2(transitive)
Updatedmyst-common@^1.1.5
Updatedmyst-spec-ext@^1.1.5