11import Node from './nodes/node' ;
22import TextNode from './nodes/text' ;
33import Document from './nodes/document' ;
4- import MarkType from './nodes/mark-type' ;
5- import { nodeToMetadata } from './Models/metadata-model' ;
4+ import { Metadata } from './Models/metadata-model' ;
65import { EntryEmbedable } from './Models/embedded-object' ;
76import { findRenderContent } from './helper/find-render-content' ;
8- import { defaultNodeOption } from './options/default-node- options' ;
9- import { Next , RenderMark , RenderNode , RenderOption } from './options ' ;
10- import { findEmbeddedItems , findRenderString } from './helper/find-embeded-object ' ;
7+ import { RenderOption } from './options' ;
8+ import { findEmbeddedItems } from './helper/find-embeded-object ' ;
9+ import { enumerate , enumerateContents } from './helper/enumerate-entries ' ;
1110
1211export type AnyNode = TextNode | Node ;
1312
@@ -17,10 +16,8 @@ export function jsonToHTML(option: {
1716 renderOption ?: RenderOption ,
1817} ) {
1918 if ( option . entry instanceof Array ) {
20- enumerateEntries ( {
21- entry : option . entry ,
22- paths : option . paths ,
23- renderOption : option . renderOption ,
19+ enumerate ( option . entry , ( entry : EntryEmbedable ) => {
20+ jsonToHTML ( { entry, paths : option . paths , renderOption : option . renderOption } )
2421 } )
2522 } else {
2623 enumerateKeys ( {
@@ -31,106 +28,16 @@ export function jsonToHTML(option: {
3128 }
3229}
3330
34- function enumerateEntries ( option : {
35- entry : EntryEmbedable [ ] ,
36- paths : string [ ] ,
37- renderOption ?: RenderOption ,
38- } ) {
39- for ( const entry of option . entry ) {
40- jsonToHTML ( { entry, paths : option . paths , renderOption : option . renderOption } )
41- }
42- }
43-
4431function enumerateKeys ( option : {
4532 entry : EntryEmbedable ,
4633 paths : string [ ] ,
4734 renderOption ?: RenderOption ,
4835} ) {
4936 for ( const key of option . paths ) {
5037 findRenderContent ( key , option . entry as EntryEmbedable , ( ( content : Document | Document [ ] ) => {
51- return enumerateContents ( content , option . entry , option . renderOption )
38+ return enumerateContents ( content , option . renderOption , ( metadata : Metadata ) => {
39+ return findEmbeddedItems ( metadata , option . entry ) [ 0 ]
40+ } )
5241 } ) )
5342 }
5443}
55-
56- function enumerateContents (
57- content :Document | Document [ ] ,
58- entry : EntryEmbedable ,
59- renderOption ?: RenderOption ,
60- ) : string | string [ ] {
61- if ( ! ( content instanceof Array ) && content . type !== 'doc' ) {
62- return content as unknown as string
63- }
64- if ( content instanceof Array ) {
65- const result : string [ ] = [ ]
66- content . forEach ( ( doc ) => {
67- result . push ( enumerateContents ( doc , entry , renderOption ) as string )
68- } )
69- return result
70- }
71- const commonRenderOption = {
72- ...defaultNodeOption ,
73- ...renderOption
74- }
75- return nodeChildrenToHTML ( content . children , commonRenderOption , entry )
76- }
77-
78- export function textNodeToHTML ( node : TextNode , renderOption : RenderOption ) : string {
79- let text = node . text
80- if ( node . superscript ) {
81- text = ( renderOption [ MarkType . SUPERSCRIPT ] as RenderMark ) ( text )
82- }
83- if ( node . subscript ) {
84- text = ( renderOption [ MarkType . SUBSCRIPT ] as RenderMark ) ( text )
85- }
86- if ( node . inlineCode ) {
87- text = ( renderOption [ MarkType . INLINE_CODE ] as RenderMark ) ( text )
88- }
89- if ( node . strikethrough ) {
90- text = ( renderOption [ MarkType . STRIKE_THROUGH ] as RenderMark ) ( text )
91- }
92- if ( node . underline ) {
93- text = ( renderOption [ MarkType . UNDERLINE ] as RenderMark ) ( text )
94- }
95- if ( node . italic ) {
96- text = ( renderOption [ MarkType . ITALIC ] as RenderMark ) ( text )
97- }
98- if ( node . bold ) {
99- text = ( renderOption [ MarkType . BOLD ] as RenderMark ) ( text )
100- }
101- return text
102- }
103-
104- export function referenceToHTML ( node : Node ,
105- renderOption : RenderOption ,
106- entry ?: EntryEmbedable
107- ) : string {
108- if ( ! entry ) {
109- return ''
110- }
111- const metadata = nodeToMetadata ( node . attrs , ( ( node . children && node . children . length > 0 ) ? node . children [ 0 ] : { } ) as unknown as TextNode )
112- const item = findEmbeddedItems ( metadata , entry ) [ 0 ]
113- return findRenderString ( item , metadata , renderOption )
114- }
115-
116- function nodeChildrenToHTML ( nodes : AnyNode [ ] ,
117- renderOption : RenderOption ,
118- entry ?: EntryEmbedable ,
119- ) : string {
120- return nodes . map < string > ( ( node : AnyNode ) => nodeToHTML ( node , renderOption , entry ) ) . join ( '' )
121- }
122-
123- function nodeToHTML (
124- node : AnyNode ,
125- renderOption : RenderOption ,
126- entry ?: EntryEmbedable ,
127- ) : string {
128- if ( ! node . type ) {
129- return textNodeToHTML ( node as TextNode , renderOption )
130- } else if ( ( node . type as string ) === 'reference' ) {
131- return referenceToHTML ( node , renderOption , entry )
132- } else {
133- const next : Next = nodes => nodeChildrenToHTML ( nodes , renderOption , entry )
134- return ( renderOption [ node . type ] as RenderNode ) ( node , next )
135- }
136- }
0 commit comments