basant307/AI_Governance_Project
045
1/**2 * @import {Effects, State, TokenType} from 'micromark-util-types'3 */4 5import {markdownSpace} from 'micromark-util-character'6 7// To do: implement `spaceOrTab`, `spaceOrTabMinMax`, `spaceOrTabWithOptions`.8 9/**10 * Parse spaces and tabs.11 *12 * There is no `nok` parameter:13 *14 * * spaces in markdown are often optional, in which case this factory can be15 * used and `ok` will be switched to whether spaces were found or not16 * * one line ending or space can be detected with `markdownSpace(code)` right17 * before using `factorySpace`18 *19 * ###### Examples20 *21 * Where `␉` represents a tab (plus how much it expands) and `␠` represents a22 * single space.23 *24 * ```markdown25 * ␉26 * ␠␠␠␠27 * ␉␠28 * ```29 *30 * @param {Effects} effects31 * Context.32 * @param {State} ok33 * State switched to when successful.34 * @param {TokenType} type35 * Type (`' \t'`).36 * @param {number | undefined} [max=Infinity]37 * Max (exclusive).38 * @returns {State}39 * Start state.40 */41export function factorySpace(effects, ok, type, max) {42 const limit = max ? max - 1 : Number.POSITIVE_INFINITY43 let size = 044 45 return start46 47 /** @type {State} */48 function start(code) {49 if (markdownSpace(code)) {50 effects.enter(type)51 return prefix(code)52 }53 54 return ok(code)55 }56 57 /** @type {State} */58 function prefix(code) {59 if (markdownSpace(code) && size++ < limit) {60 effects.consume(code)61 return prefix62 }63 64 effects.exit(type)65 return ok(code)66 }67}68 