document-ir 0.0.13 → 0.0.15

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,4 +1,4 @@
1
- import { ArrayNode, BlockNode, BlockQuoteNode, BoldNode, BreakNode, BubbleNode, CardNode, CenterNode, CodeNode, ColumnsNode, DateNode, DateTimeNode, DefinitionListNode, DefinitionNode, DefinitionReferenceNode, DocumentNode, EmbedNode, EmojiNode, FigureCaptionNode, FigureImageNode, FigureNode, FormattedTextNode, HeaderNode, HighTechAlertNode, HorizontalRuleNode, ImageNode, ItalicNode, LinkNode, ListNode, Node, NoteNode, ParagraphNode, QuoteNode, RedactedNode, RegionNode, ScriptNode, SecretNode, SmallerNode, SocialNode, StickerNode, StrikeThroughNode, SubTextNode, SuperTextNode, TableNode, TextNode, TimeNode, UnderlineNode, VideoNode, WarningNode } from "./types.js";
1
+ import { ArrayNode, BlockNode, BlockQuoteNode, BoldNode, BreakNode, BubbleNode, CardNode, CenterNode, CodeNode, ColumnsNode, DateNode, DateTimeNode, DefinitionListNode, DefinitionNode, DefinitionReferenceNode, DocumentNode, EmbedNode, EmojiNode, FigureCaptionNode, FigureImageNode, FigureNode, FormattedTextNode, HeaderNode, HighTechAlertNode, HorizontalRuleNode, ImageNode, ItalicNode, LinkNode, ListNode, Node, NoteNode, ParagraphNode, QuoteNode, RedactedNode, RegionNode, ScriptNode, SecretNode, SmallerNode, SocialNode, StickerNode, StrikeThroughNode, SubTextNode, SuperTextNode, TableNode, TableOfContentsNode, TextNode, TimeNode, UnderlineNode, VideoNode, WarningNode } from "./types.js";
2
2
  export declare class IdentityTransformer {
3
3
  protected beforeBlock(): Promise<void>;
4
4
  protected afterBlock(): Promise<void>;
@@ -52,6 +52,7 @@ export declare class IdentityTransformer {
52
52
  protected datetime(node: DateTimeNode): Promise<Node | null>;
53
53
  protected subText(node: SubTextNode): Promise<Node | null>;
54
54
  protected superText(node: SuperTextNode): Promise<Node | null>;
55
+ protected toc(node: TableOfContentsNode): Promise<Node | null>;
55
56
  protected choose(node: Node): Promise<Node | null>;
56
57
  protected document(node: DocumentNode): Promise<DocumentNode>;
57
58
  transform(node: DocumentNode): Promise<DocumentNode>;
@@ -669,6 +669,32 @@ export class IdentityTransformer {
669
669
  content,
670
670
  };
671
671
  }
672
+ async toc(node) {
673
+ await this.beforeBlock();
674
+ const content = await this.chooseChildren(node.content);
675
+ const date = node.date && await this.choose(node.date);
676
+ await this.afterBlock();
677
+ await this.beforeBlock();
678
+ const mixedChildren = await this.chooseChildren(node.children);
679
+ await this.afterBlock();
680
+ const children = mixedChildren.filter((x) => x.type == "toc");
681
+ const result = {
682
+ type: "toc",
683
+ content,
684
+ children,
685
+ };
686
+ if (date &&
687
+ (date.type == "date" || date.type == "time" || date.type == "datetime")) {
688
+ result.date = date;
689
+ }
690
+ if (node.href) {
691
+ result.href = node.href;
692
+ }
693
+ if (node.hrefHtmlId) {
694
+ result.hrefHtmlId = node.hrefHtmlId;
695
+ }
696
+ return result;
697
+ }
672
698
  async choose(node) {
673
699
  if (!node || !node.type) {
674
700
  throw new Error(`Unexpected node, no type: ${JSON.stringify(node)}`);
@@ -772,6 +798,8 @@ export class IdentityTransformer {
772
798
  return await this.superText(node);
773
799
  case "sub":
774
800
  return await this.subText(node);
801
+ case "toc":
802
+ return await this.toc(node);
775
803
  }
776
804
  }
777
805
  catch (e) {
@@ -827,6 +855,9 @@ export class IdentityTransformer {
827
855
  if (node.hidden) {
828
856
  result.hidden = node.hidden;
829
857
  }
858
+ if (node.readingDifficultyMultiplier) {
859
+ result.readingDifficultyMultiplier = node.readingDifficultyMultiplier;
860
+ }
830
861
  return result;
831
862
  }
832
863
  async transform(node) {
@@ -1,4 +1,4 @@
1
- import { ArrayNode, BlockNode, BlockQuoteNode, BoldNode, BreakNode, BubbleNode, CardNode, CenterNode, CodeNode, ColumnsNode, DateNode, DateTimeNode, DefinitionListNode, DefinitionNode, DefinitionReferenceNode, DocumentNode, EmbedNode, EmojiNode, FigureCaptionNode, FigureImageNode, FigureNode, FormattedTextNode, HeaderNode, HighTechAlertNode, HorizontalRuleNode, ImageNode, ItalicNode, LinkNode, ListNode, Node, NoteNode, ParagraphNode, QuoteNode, RedactedNode, RegionNode, ScriptNode, SecretNode, SmallerNode, SocialNode, StickerNode, StrikeThroughNode, SubTextNode, SuperTextNode, TableNode, TextNode, TimeNode, UnderlineNode, VideoNode, WarningNode } from "./types.js";
1
+ import { ArrayNode, BlockNode, BlockQuoteNode, BoldNode, BreakNode, BubbleNode, CardNode, CenterNode, CodeNode, ColumnsNode, DateNode, DateTimeNode, DefinitionListNode, DefinitionNode, DefinitionReferenceNode, DocumentNode, EmbedNode, EmojiNode, FigureCaptionNode, FigureImageNode, FigureNode, FormattedTextNode, HeaderNode, HighTechAlertNode, HorizontalRuleNode, ImageNode, ItalicNode, LinkNode, ListNode, Node, NoteNode, ParagraphNode, QuoteNode, RedactedNode, RegionNode, ScriptNode, SecretNode, SmallerNode, SocialNode, StickerNode, StrikeThroughNode, SubTextNode, SuperTextNode, TableNode, TableOfContentsNode, TextNode, TimeNode, UnderlineNode, VideoNode, WarningNode } from "./types.js";
2
2
  export declare class NodeVisitor {
3
3
  protected beforeBlock(): void;
4
4
  protected afterBlock(): void;
@@ -52,6 +52,7 @@ export declare class NodeVisitor {
52
52
  protected datetime(node: DateTimeNode): void;
53
53
  protected subText(node: SubTextNode): void;
54
54
  protected superText(node: SuperTextNode): void;
55
+ protected toc(node: TableOfContentsNode): void;
55
56
  protected choose(node: Node): void;
56
57
  protected document(node: DocumentNode): void;
57
58
  visit(node: DocumentNode | Node): void;
@@ -271,6 +271,14 @@ export class NodeVisitor {
271
271
  this.chooseChildren(node.content);
272
272
  this.afterInline();
273
273
  }
274
+ toc(node) {
275
+ this.beforeBlock();
276
+ this.chooseChildren(node.content);
277
+ this.afterBlock();
278
+ this.beforeBlock();
279
+ this.chooseChildren(node.children);
280
+ this.afterBlock();
281
+ }
274
282
  choose(node) {
275
283
  if (!node || !node.type) {
276
284
  throw new Error(`Unexpected node, no type: ${JSON.stringify(node)}`);
@@ -374,6 +382,8 @@ export class NodeVisitor {
374
382
  return this.subText(node);
375
383
  case "super":
376
384
  return this.superText(node);
385
+ case "toc":
386
+ return this.toc(node);
377
387
  }
378
388
  }
379
389
  catch (e) {
@@ -1,4 +1,4 @@
1
- import { DefinitionNode, DefinitionReferenceNode, EmojiNode, FigureImageNode, ImageNode, TextNode, VideoNode } from "./types.js";
1
+ import { DefinitionNode, DefinitionReferenceNode, EmojiNode, FigureImageNode, ImageNode, TableOfContentsNode, TextNode, VideoNode } from "./types.js";
2
2
  import { NodeVisitor } from "./NodeVisitor.js";
3
3
  export declare class TextVisitor extends NodeVisitor {
4
4
  private textList;
@@ -10,5 +10,6 @@ export declare class TextVisitor extends NodeVisitor {
10
10
  protected figureImage(node: FigureImageNode): void;
11
11
  protected definitionReference(node: DefinitionReferenceNode): void;
12
12
  protected definition(node: DefinitionNode): void;
13
+ protected toc(node: TableOfContentsNode): void;
13
14
  getText(): string;
14
15
  }
@@ -40,6 +40,15 @@ export class TextVisitor extends NodeVisitor {
40
40
  this.textList.push("): ");
41
41
  this.chooseChildren(node.content);
42
42
  }
43
+ toc(node) {
44
+ if (node.date) {
45
+ this.choose(node.date);
46
+ this.textList.push(" ");
47
+ }
48
+ this.chooseChildren(node.content);
49
+ this.textList.push("\n");
50
+ this.chooseChildren(node.children);
51
+ }
43
52
  getText() {
44
53
  return this.textList.join("");
45
54
  }
package/esm/types.d.ts CHANGED
@@ -313,7 +313,15 @@ export interface DateTimeNode {
313
313
  iso8601: string;
314
314
  content: Node[];
315
315
  }
316
- export type Node = ArrayNode | BlockNode | BlockQuoteNode | BoldNode | BreakNode | BubbleNode | CardNode | CenterNode | CodeNode | ColumnsNode | DefinitionNode | DefinitionListNode | DefinitionReferenceNode | EmbedNode | EmojiNode | FigureNode | FigureCaptionNode | FigureImageNode | FormattedTextNode | HeaderNode | HighTechAlertNode | HorizontalRuleNode | ImageNode | ItalicNode | LinkNode | ListNode | NoteNode | ParagraphNode | QuoteNode | RedactedNode | RegionNode | ScriptNode | SecretNode | SmallerNode | StickerNode | StrikeThroughNode | TextNode | TableNode | SocialNode | UnderlineNode | VideoNode | DateNode | TimeNode | DateTimeNode | SuperTextNode | SubTextNode | WarningNode;
316
+ export interface TableOfContentsNode {
317
+ type: "toc";
318
+ date?: DateTimeNode | DateNode | TimeNode;
319
+ href?: string;
320
+ hrefHtmlId?: string;
321
+ content: Node[];
322
+ children: TableOfContentsNode[];
323
+ }
324
+ export type Node = ArrayNode | BlockNode | BlockQuoteNode | BoldNode | BreakNode | BubbleNode | CardNode | CenterNode | CodeNode | ColumnsNode | DefinitionNode | DefinitionListNode | DefinitionReferenceNode | EmbedNode | EmojiNode | FigureNode | FigureCaptionNode | FigureImageNode | FormattedTextNode | HeaderNode | HighTechAlertNode | HorizontalRuleNode | ImageNode | ItalicNode | LinkNode | ListNode | NoteNode | ParagraphNode | QuoteNode | RedactedNode | RegionNode | ScriptNode | SecretNode | SmallerNode | StickerNode | StrikeThroughNode | TextNode | TableNode | SocialNode | UnderlineNode | VideoNode | DateNode | TimeNode | DateTimeNode | SuperTextNode | SubTextNode | TableOfContentsNode | WarningNode;
317
325
  export interface DocumentMeta {
318
326
  hidden?: boolean;
319
327
  noindex?: boolean;
@@ -326,6 +334,7 @@ export interface DocumentMeta {
326
334
  date?: string;
327
335
  url: string;
328
336
  contentDigest?: string;
337
+ readingDifficultyMultiplier?: number;
329
338
  }
330
339
  export interface DocumentHierarchy {
331
340
  headerText: string;
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "module": "./esm/index.js",
3
3
  "main": "./script/index.js",
4
4
  "name": "document-ir",
5
- "version": "0.0.13",
5
+ "version": "0.0.15",
6
6
  "description": "Intermediate representation and transformers for documents",
7
7
  "license": "MIT",
8
8
  "repository": {
@@ -1,4 +1,4 @@
1
- import { ArrayNode, BlockNode, BlockQuoteNode, BoldNode, BreakNode, BubbleNode, CardNode, CenterNode, CodeNode, ColumnsNode, DateNode, DateTimeNode, DefinitionListNode, DefinitionNode, DefinitionReferenceNode, DocumentNode, EmbedNode, EmojiNode, FigureCaptionNode, FigureImageNode, FigureNode, FormattedTextNode, HeaderNode, HighTechAlertNode, HorizontalRuleNode, ImageNode, ItalicNode, LinkNode, ListNode, Node, NoteNode, ParagraphNode, QuoteNode, RedactedNode, RegionNode, ScriptNode, SecretNode, SmallerNode, SocialNode, StickerNode, StrikeThroughNode, SubTextNode, SuperTextNode, TableNode, TextNode, TimeNode, UnderlineNode, VideoNode, WarningNode } from "./types.js";
1
+ import { ArrayNode, BlockNode, BlockQuoteNode, BoldNode, BreakNode, BubbleNode, CardNode, CenterNode, CodeNode, ColumnsNode, DateNode, DateTimeNode, DefinitionListNode, DefinitionNode, DefinitionReferenceNode, DocumentNode, EmbedNode, EmojiNode, FigureCaptionNode, FigureImageNode, FigureNode, FormattedTextNode, HeaderNode, HighTechAlertNode, HorizontalRuleNode, ImageNode, ItalicNode, LinkNode, ListNode, Node, NoteNode, ParagraphNode, QuoteNode, RedactedNode, RegionNode, ScriptNode, SecretNode, SmallerNode, SocialNode, StickerNode, StrikeThroughNode, SubTextNode, SuperTextNode, TableNode, TableOfContentsNode, TextNode, TimeNode, UnderlineNode, VideoNode, WarningNode } from "./types.js";
2
2
  export declare class IdentityTransformer {
3
3
  protected beforeBlock(): Promise<void>;
4
4
  protected afterBlock(): Promise<void>;
@@ -52,6 +52,7 @@ export declare class IdentityTransformer {
52
52
  protected datetime(node: DateTimeNode): Promise<Node | null>;
53
53
  protected subText(node: SubTextNode): Promise<Node | null>;
54
54
  protected superText(node: SuperTextNode): Promise<Node | null>;
55
+ protected toc(node: TableOfContentsNode): Promise<Node | null>;
55
56
  protected choose(node: Node): Promise<Node | null>;
56
57
  protected document(node: DocumentNode): Promise<DocumentNode>;
57
58
  transform(node: DocumentNode): Promise<DocumentNode>;
@@ -672,6 +672,32 @@ class IdentityTransformer {
672
672
  content,
673
673
  };
674
674
  }
675
+ async toc(node) {
676
+ await this.beforeBlock();
677
+ const content = await this.chooseChildren(node.content);
678
+ const date = node.date && await this.choose(node.date);
679
+ await this.afterBlock();
680
+ await this.beforeBlock();
681
+ const mixedChildren = await this.chooseChildren(node.children);
682
+ await this.afterBlock();
683
+ const children = mixedChildren.filter((x) => x.type == "toc");
684
+ const result = {
685
+ type: "toc",
686
+ content,
687
+ children,
688
+ };
689
+ if (date &&
690
+ (date.type == "date" || date.type == "time" || date.type == "datetime")) {
691
+ result.date = date;
692
+ }
693
+ if (node.href) {
694
+ result.href = node.href;
695
+ }
696
+ if (node.hrefHtmlId) {
697
+ result.hrefHtmlId = node.hrefHtmlId;
698
+ }
699
+ return result;
700
+ }
675
701
  async choose(node) {
676
702
  if (!node || !node.type) {
677
703
  throw new Error(`Unexpected node, no type: ${JSON.stringify(node)}`);
@@ -775,6 +801,8 @@ class IdentityTransformer {
775
801
  return await this.superText(node);
776
802
  case "sub":
777
803
  return await this.subText(node);
804
+ case "toc":
805
+ return await this.toc(node);
778
806
  }
779
807
  }
780
808
  catch (e) {
@@ -830,6 +858,9 @@ class IdentityTransformer {
830
858
  if (node.hidden) {
831
859
  result.hidden = node.hidden;
832
860
  }
861
+ if (node.readingDifficultyMultiplier) {
862
+ result.readingDifficultyMultiplier = node.readingDifficultyMultiplier;
863
+ }
833
864
  return result;
834
865
  }
835
866
  async transform(node) {
@@ -1,4 +1,4 @@
1
- import { ArrayNode, BlockNode, BlockQuoteNode, BoldNode, BreakNode, BubbleNode, CardNode, CenterNode, CodeNode, ColumnsNode, DateNode, DateTimeNode, DefinitionListNode, DefinitionNode, DefinitionReferenceNode, DocumentNode, EmbedNode, EmojiNode, FigureCaptionNode, FigureImageNode, FigureNode, FormattedTextNode, HeaderNode, HighTechAlertNode, HorizontalRuleNode, ImageNode, ItalicNode, LinkNode, ListNode, Node, NoteNode, ParagraphNode, QuoteNode, RedactedNode, RegionNode, ScriptNode, SecretNode, SmallerNode, SocialNode, StickerNode, StrikeThroughNode, SubTextNode, SuperTextNode, TableNode, TextNode, TimeNode, UnderlineNode, VideoNode, WarningNode } from "./types.js";
1
+ import { ArrayNode, BlockNode, BlockQuoteNode, BoldNode, BreakNode, BubbleNode, CardNode, CenterNode, CodeNode, ColumnsNode, DateNode, DateTimeNode, DefinitionListNode, DefinitionNode, DefinitionReferenceNode, DocumentNode, EmbedNode, EmojiNode, FigureCaptionNode, FigureImageNode, FigureNode, FormattedTextNode, HeaderNode, HighTechAlertNode, HorizontalRuleNode, ImageNode, ItalicNode, LinkNode, ListNode, Node, NoteNode, ParagraphNode, QuoteNode, RedactedNode, RegionNode, ScriptNode, SecretNode, SmallerNode, SocialNode, StickerNode, StrikeThroughNode, SubTextNode, SuperTextNode, TableNode, TableOfContentsNode, TextNode, TimeNode, UnderlineNode, VideoNode, WarningNode } from "./types.js";
2
2
  export declare class NodeVisitor {
3
3
  protected beforeBlock(): void;
4
4
  protected afterBlock(): void;
@@ -52,6 +52,7 @@ export declare class NodeVisitor {
52
52
  protected datetime(node: DateTimeNode): void;
53
53
  protected subText(node: SubTextNode): void;
54
54
  protected superText(node: SuperTextNode): void;
55
+ protected toc(node: TableOfContentsNode): void;
55
56
  protected choose(node: Node): void;
56
57
  protected document(node: DocumentNode): void;
57
58
  visit(node: DocumentNode | Node): void;
@@ -274,6 +274,14 @@ class NodeVisitor {
274
274
  this.chooseChildren(node.content);
275
275
  this.afterInline();
276
276
  }
277
+ toc(node) {
278
+ this.beforeBlock();
279
+ this.chooseChildren(node.content);
280
+ this.afterBlock();
281
+ this.beforeBlock();
282
+ this.chooseChildren(node.children);
283
+ this.afterBlock();
284
+ }
277
285
  choose(node) {
278
286
  if (!node || !node.type) {
279
287
  throw new Error(`Unexpected node, no type: ${JSON.stringify(node)}`);
@@ -377,6 +385,8 @@ class NodeVisitor {
377
385
  return this.subText(node);
378
386
  case "super":
379
387
  return this.superText(node);
388
+ case "toc":
389
+ return this.toc(node);
380
390
  }
381
391
  }
382
392
  catch (e) {
@@ -1,4 +1,4 @@
1
- import { DefinitionNode, DefinitionReferenceNode, EmojiNode, FigureImageNode, ImageNode, TextNode, VideoNode } from "./types.js";
1
+ import { DefinitionNode, DefinitionReferenceNode, EmojiNode, FigureImageNode, ImageNode, TableOfContentsNode, TextNode, VideoNode } from "./types.js";
2
2
  import { NodeVisitor } from "./NodeVisitor.js";
3
3
  export declare class TextVisitor extends NodeVisitor {
4
4
  private textList;
@@ -10,5 +10,6 @@ export declare class TextVisitor extends NodeVisitor {
10
10
  protected figureImage(node: FigureImageNode): void;
11
11
  protected definitionReference(node: DefinitionReferenceNode): void;
12
12
  protected definition(node: DefinitionNode): void;
13
+ protected toc(node: TableOfContentsNode): void;
13
14
  getText(): string;
14
15
  }
@@ -43,6 +43,15 @@ class TextVisitor extends NodeVisitor_js_1.NodeVisitor {
43
43
  this.textList.push("): ");
44
44
  this.chooseChildren(node.content);
45
45
  }
46
+ toc(node) {
47
+ if (node.date) {
48
+ this.choose(node.date);
49
+ this.textList.push(" ");
50
+ }
51
+ this.chooseChildren(node.content);
52
+ this.textList.push("\n");
53
+ this.chooseChildren(node.children);
54
+ }
46
55
  getText() {
47
56
  return this.textList.join("");
48
57
  }
package/script/types.d.ts CHANGED
@@ -313,7 +313,15 @@ export interface DateTimeNode {
313
313
  iso8601: string;
314
314
  content: Node[];
315
315
  }
316
- export type Node = ArrayNode | BlockNode | BlockQuoteNode | BoldNode | BreakNode | BubbleNode | CardNode | CenterNode | CodeNode | ColumnsNode | DefinitionNode | DefinitionListNode | DefinitionReferenceNode | EmbedNode | EmojiNode | FigureNode | FigureCaptionNode | FigureImageNode | FormattedTextNode | HeaderNode | HighTechAlertNode | HorizontalRuleNode | ImageNode | ItalicNode | LinkNode | ListNode | NoteNode | ParagraphNode | QuoteNode | RedactedNode | RegionNode | ScriptNode | SecretNode | SmallerNode | StickerNode | StrikeThroughNode | TextNode | TableNode | SocialNode | UnderlineNode | VideoNode | DateNode | TimeNode | DateTimeNode | SuperTextNode | SubTextNode | WarningNode;
316
+ export interface TableOfContentsNode {
317
+ type: "toc";
318
+ date?: DateTimeNode | DateNode | TimeNode;
319
+ href?: string;
320
+ hrefHtmlId?: string;
321
+ content: Node[];
322
+ children: TableOfContentsNode[];
323
+ }
324
+ export type Node = ArrayNode | BlockNode | BlockQuoteNode | BoldNode | BreakNode | BubbleNode | CardNode | CenterNode | CodeNode | ColumnsNode | DefinitionNode | DefinitionListNode | DefinitionReferenceNode | EmbedNode | EmojiNode | FigureNode | FigureCaptionNode | FigureImageNode | FormattedTextNode | HeaderNode | HighTechAlertNode | HorizontalRuleNode | ImageNode | ItalicNode | LinkNode | ListNode | NoteNode | ParagraphNode | QuoteNode | RedactedNode | RegionNode | ScriptNode | SecretNode | SmallerNode | StickerNode | StrikeThroughNode | TextNode | TableNode | SocialNode | UnderlineNode | VideoNode | DateNode | TimeNode | DateTimeNode | SuperTextNode | SubTextNode | TableOfContentsNode | WarningNode;
317
325
  export interface DocumentMeta {
318
326
  hidden?: boolean;
319
327
  noindex?: boolean;
@@ -326,6 +334,7 @@ export interface DocumentMeta {
326
334
  date?: string;
327
335
  url: string;
328
336
  contentDigest?: string;
337
+ readingDifficultyMultiplier?: number;
329
338
  }
330
339
  export interface DocumentHierarchy {
331
340
  headerText: string;