@pretextbook/remark-pretext 0.0.6 → 0.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
  import { BlockContent, DefinitionContent, PhrasingContent } from 'mdast';
2
- import { DivisionType } from '@pretextbook/ptxast';
2
+ import { RootDivisionType, TopLevelDivisionType } from '@pretextbook/ptxast';
3
3
  export interface VisitContext {
4
4
  /** Parent node, if any. */
5
5
  parent?: BlockContent | DefinitionContent | PhrasingContent;
@@ -12,7 +12,19 @@ export interface VisitContext {
12
12
  /** Raw source markdown (optional, used for delimiter detection). */
13
13
  source?: string;
14
14
  /** The division type that a depth-1 heading (`#`) maps to. */
15
- topLevelDivision: DivisionType;
15
+ topLevelDivision: TopLevelDivisionType;
16
+ /** When set, the whole document is wrapped in this root element
17
+ * (`book`/`article`/`slideshow`) and `topLevelDivision` is the root's
18
+ * outermost child division. */
19
+ documentRoot?: RootDivisionType;
20
+ /** Attributes (e.g. from frontmatter `xmlid`/`label`/`component`) to apply
21
+ * to the first root-level division built from the document. */
22
+ topLevelAttributes?: Record<string, string>;
23
+ /** Mutable: set once `topLevelAttributes` has been applied to a division,
24
+ * shared by reference across the recursive conversion of this document. */
25
+ topLevelAttributesApplied?: {
26
+ done: boolean;
27
+ };
16
28
  }
17
29
  export interface ConversionMessage {
18
30
  type: 'warning' | 'error';
@@ -1,7 +1,13 @@
1
- import { DivisionType } from '@pretextbook/ptxast';
1
+ import { RootDivisionType, TopLevelDivisionType } from '@pretextbook/ptxast';
2
2
  export interface FrontmatterResult {
3
- /** The declared top-level division type, if present and valid. */
4
- division?: DivisionType;
3
+ /** The division a depth-1 heading (`#`) maps to, if present and valid. When
4
+ * `documentRoot` is set this is the root's outermost child division. */
5
+ division?: TopLevelDivisionType;
6
+ /** The document root (`book`/`article`/`slideshow`) to wrap the whole
7
+ * document in, when `division:` names one. */
8
+ documentRoot?: RootDivisionType;
9
+ /** Attributes (`xml:id`, `label`, `component`) for the top-level division. */
10
+ attributes?: Record<string, string>;
5
11
  /** The markdown source with the frontmatter block removed. */
6
12
  body: string;
7
13
  }
@@ -1,7 +1,7 @@
1
1
  import { Root as MdastRoot } from 'mdast';
2
2
  import { Root } from 'xast';
3
3
  import { ConversionMessage } from './context.js';
4
- import { DivisionType } from '@pretextbook/ptxast';
4
+ import { RootDivisionType, TopLevelDivisionType } from '@pretextbook/ptxast';
5
5
  /** Result type for conversion: tree + diagnostic messages. */
6
6
  export interface ConversionResult {
7
7
  tree: Root;
@@ -10,7 +10,14 @@ export interface ConversionResult {
10
10
  /** Options controlling heading-to-division conversion. */
11
11
  export interface MdastToPtxastOptions {
12
12
  /** The division type that a depth-1 heading (`#`) maps to. Defaults to `'chapter'`. */
13
- topLevelDivision?: DivisionType;
13
+ topLevelDivision?: TopLevelDivisionType;
14
+ /** When set, wrap the whole document in this root element
15
+ * (`book`/`article`/`slideshow`); `topLevelDivision` is then the division a
16
+ * depth-1 heading maps to inside the root. */
17
+ documentRoot?: RootDivisionType;
18
+ /** Attributes (`xml:id`, `label`, `component`) applied to the first
19
+ * root-level division built from the document, e.g. from frontmatter. */
20
+ topLevelAttributes?: Record<string, string>;
14
21
  }
15
22
  /** Transform an mdast Root node into an xast Root node (backwards compatible). */
16
23
  export declare function mdastToPtxast(tree: MdastRoot, source?: string, options?: MdastToPtxastOptions): Root;
@@ -0,0 +1,27 @@
1
+ import { Element } from 'xast';
2
+ import { ContainerDirective, LeafDirective, TextDirective } from 'mdast-util-directive';
3
+ /** Any of the three remark-directive node shapes (`:x`, `::x`, `:::x`). */
4
+ type DirectiveNode = TextDirective | LeafDirective | ContainerDirective;
5
+ /**
6
+ * Build a `<plus:KIND .../>` include element from a kind name and attributes.
7
+ *
8
+ * This is the one choke point every include syntax funnels through: the tag is
9
+ * always `plus:<kind>`, the element is always empty (a reference, never
10
+ * transcluded), and `null`/`undefined` attribute values are dropped so a
11
+ * value-less directive attribute doesn't emit `key="null"`.
12
+ */
13
+ export declare function plusIncludeElement(kind: string, attributes?: Record<string, string | null | undefined>): Element;
14
+ /**
15
+ * Interpret a remark-directive node as a PreTeXt Plus include: the directive
16
+ * name becomes the `plus:` kind and its attributes pass through verbatim
17
+ * (`::section{ref="x"}` → `<plus:section ref="x"/>`).
18
+ *
19
+ * Attributes are intentionally *not* remapped the way block directives remap
20
+ * `id`→`xml:id`: an include points at another element by `ref`, so its
21
+ * attributes are kept exactly as written.
22
+ *
23
+ * Returns `null` when the node carries no usable name, so callers can fall back
24
+ * to their normal handling rather than emitting `<plus:/>`.
25
+ */
26
+ export declare function directiveToPlusInclude(node: DirectiveNode): Element | null;
27
+ export {};
@@ -1,7 +1,7 @@
1
1
  import { Plugin } from 'unified';
2
2
  import { Root as MdastRoot } from 'mdast';
3
3
  import { Root } from 'xast';
4
- import { DivisionType } from '@pretextbook/ptxast';
4
+ import { RootDivisionType, TopLevelDivisionType } from '@pretextbook/ptxast';
5
5
  /** Options for the remark-pretext plugin. */
6
6
  export interface RemarkPretextOptions {
7
7
  /**
@@ -9,7 +9,20 @@ export interface RemarkPretextOptions {
9
9
  * `division:` field declared in the markdown's frontmatter. Defaults to
10
10
  * `'chapter'` (matching historical behavior) when neither is set.
11
11
  */
12
- topLevelDivision?: DivisionType;
12
+ topLevelDivision?: TopLevelDivisionType;
13
+ /**
14
+ * Wraps the whole document in this root element (`book`/`article`/
15
+ * `slideshow`). Overrides any document root declared via the frontmatter
16
+ * `division:` field. When set, `topLevelDivision` is the division a depth-1
17
+ * heading maps to *inside* the root.
18
+ */
19
+ documentRoot?: RootDivisionType;
20
+ /**
21
+ * Attributes (`xml:id`, `label`, `component`) applied to the first
22
+ * root-level division. Overrides any `xmlid:`/`label:`/`component:`
23
+ * fields declared in the markdown's frontmatter.
24
+ */
25
+ topLevelAttributes?: Record<string, string>;
13
26
  }
14
27
  declare const remarkPretext: Plugin<[RemarkPretextOptions?], MdastRoot, Root>;
15
28
  export default remarkPretext;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pretextbook/remark-pretext",
3
- "version": "0.0.6",
3
+ "version": "0.0.8",
4
4
  "description": "remark plugin to transform markdown-style PreTeXt directives into ptxast",
5
5
  "author": "Oscar Levin",
6
6
  "license": "MIT",
@@ -39,7 +39,7 @@
39
39
  "test": "vitest run"
40
40
  },
41
41
  "dependencies": {
42
- "@pretextbook/ptxast": "^0.0.5",
42
+ "@pretextbook/ptxast": "^0.0.7",
43
43
  "@types/mdast": "^4.0.0",
44
44
  "mdast-util-directive": "^3.0.0",
45
45
  "remark-directive": "^4.0.0",