@@ -3,6 +3,18 @@ import { timeout } from './utils.js';
33import { describe , it , beforeEach } from 'node:test' ;
44import assert from 'node:assert' ;
55
6+ function createHeadingToken ( text ) {
7+ return {
8+ type : 'heading' ,
9+ raw : `# ${ text } ` ,
10+ depth : 1 ,
11+ text,
12+ tokens : [
13+ { type : 'text' , raw : text , text }
14+ ]
15+ } ;
16+ }
17+
618describe ( 'Hooks' , ( ) => {
719 let marked ;
820 beforeEach ( ( ) => {
@@ -93,6 +105,48 @@ describe('Hooks', () => {
93105 assert . strictEqual ( html . trim ( ) , '<p><em>text</em></p>\n<h1>postprocess async</h1>' ) ;
94106 } ) ;
95107
108+ it ( 'should process tokens before walkTokens' , ( ) => {
109+ marked . use ( {
110+ hooks : {
111+ processAllTokens ( tokens ) {
112+ tokens . push ( createHeadingToken ( 'processAllTokens' ) ) ;
113+ return tokens ;
114+ }
115+ } ,
116+ walkTokens ( token ) {
117+ if ( token . type === 'heading' ) {
118+ token . tokens [ 0 ] . text += ' walked' ;
119+ }
120+ return token ;
121+ }
122+ } ) ;
123+ const html = marked . parse ( '*text*' ) ;
124+ assert . strictEqual ( html . trim ( ) , '<p><em>text</em></p>\n<h1>processAllTokens walked</h1>' ) ;
125+ } ) ;
126+
127+ it ( 'should process tokens async before walkTokens' , async ( ) => {
128+ marked . use ( {
129+ async : true ,
130+ hooks : {
131+ async processAllTokens ( tokens ) {
132+ await timeout ( ) ;
133+ tokens . push ( createHeadingToken ( 'processAllTokens async' ) ) ;
134+ return tokens ;
135+ }
136+ } ,
137+ walkTokens ( token ) {
138+ if ( token . type === 'heading' ) {
139+ token . tokens [ 0 ] . text += ' walked' ;
140+ }
141+ return token ;
142+ }
143+ } ) ;
144+ const promise = marked . parse ( '*text*' ) ;
145+ assert . ok ( promise instanceof Promise ) ;
146+ const html = await promise ;
147+ assert . strictEqual ( html . trim ( ) , '<p><em>text</em></p>\n<h1>processAllTokens async walked</h1>' ) ;
148+ } ) ;
149+
96150 it ( 'should process all hooks in reverse' , async ( ) => {
97151 marked . use ( {
98152 hooks : {
@@ -101,6 +155,10 @@ describe('Hooks', () => {
101155 } ,
102156 postprocess ( html ) {
103157 return html + '<h1>postprocess1</h1>\n' ;
158+ } ,
159+ processAllTokens ( tokens ) {
160+ tokens . push ( createHeadingToken ( 'processAllTokens1' ) ) ;
161+ return tokens ;
104162 }
105163 }
106164 } ) ;
@@ -113,12 +171,23 @@ describe('Hooks', () => {
113171 async postprocess ( html ) {
114172 await timeout ( ) ;
115173 return html + '<h1>postprocess2 async</h1>\n' ;
174+ } ,
175+ processAllTokens ( tokens ) {
176+ tokens . push ( createHeadingToken ( 'processAllTokens2' ) ) ;
177+ return tokens ;
116178 }
117179 }
118180 } ) ;
119181 const promise = marked . parse ( '*text*' ) ;
120182 assert . ok ( promise instanceof Promise ) ;
121183 const html = await promise ;
122- assert . strictEqual ( html . trim ( ) , '<h1>preprocess1</h1>\n<h1>preprocess2</h1>\n<p><em>text</em></p>\n<h1>postprocess2 async</h1>\n<h1>postprocess1</h1>' ) ;
184+ assert . strictEqual ( html . trim ( ) , `\
185+ <h1>preprocess1</h1>
186+ <h1>preprocess2</h1>
187+ <p><em>text</em></p>
188+ <h1>processAllTokens2</h1>
189+ <h1>processAllTokens1</h1>
190+ <h1>postprocess2 async</h1>
191+ <h1>postprocess1</h1>` ) ;
123192 } ) ;
124193} ) ;
0 commit comments