|
1 | 1 | // based on https://github.com/syntax-tree/hast-util-to-mdast/blob/main/lib/handlers/em |
2 | 2 |
|
3 | | -import { H, Body, Handle } from '../types' |
| 3 | +import { H, Body, Handle, P, UnifiedLatexNode } from '../types' |
4 | 4 | import { all } from '../all' |
| 5 | +import { getPStyle } from '../util/get-pstyle' |
| 6 | +import { getListInfo } from '../util/get-listinfo' |
| 7 | +import { one } from '../one' |
| 8 | +import { Element } from 'xast-util-to-string/lib' |
| 9 | +import { |
| 10 | + Argument, |
| 11 | + Environment, |
| 12 | + Macro, |
| 13 | + Verb, |
| 14 | + Whitespace, |
| 15 | +} from '@unified-latex/unified-latex-types' |
| 16 | +import { WhiteSpace } from 'nlcst' |
| 17 | +import { arg, m, SP } from '@unified-latex/unified-latex-builder' |
| 18 | +import { PB } from '../util/PB' |
| 19 | +import { updateRenderInfo } from '@unified-latex/unified-latex-util-render-info' |
| 20 | + |
| 21 | +const isP = (node: Element): node is P => |
| 22 | + node.type === 'element' && node.name === 'w:p' |
5 | 23 |
|
6 | 24 | export const body: Handle = (h: H, body: Body) => { |
7 | 25 | const bod = all(h, body) |
8 | 26 |
|
9 | | - return bod |
| 27 | + const processedBody = body.children.reduce((acc, child, index) => { |
| 28 | + if (child.type !== 'element') return acc |
| 29 | + |
| 30 | + if (!isP(child)) { |
| 31 | + acc.push(...makeOne(h, child, body)) |
| 32 | + return acc |
| 33 | + } |
| 34 | + |
| 35 | + const isListItem = getPStyle(child) === 'ListParagraph' |
| 36 | + |
| 37 | + if (!isListItem) { |
| 38 | + acc.push(...makeOne(h, child, body)) |
| 39 | + return acc |
| 40 | + } |
| 41 | + |
| 42 | + const { ilvl, numId } = getListInfo(child) ?? {} |
| 43 | + |
| 44 | + if (ilvl == null || numId == null) { |
| 45 | + acc.push(...makeOne(h, child, body)) |
| 46 | + return acc |
| 47 | + } |
| 48 | + |
| 49 | + const prevChild = body.children[index - 1] |
| 50 | + |
| 51 | + const isPrevListItem = |
| 52 | + prevChild && isP(prevChild) && getPStyle(prevChild) === 'ListParagraph' |
| 53 | + |
| 54 | + const { ilvl: prevIlvl, numId: prevNumId } = |
| 55 | + (isPrevListItem && getListInfo(prevChild)) || {} |
| 56 | + |
| 57 | + const listItem = makeItem(h, child) |
| 58 | + |
| 59 | + // there's no previous list item, we need to create a new environment |
| 60 | + if (!isPrevListItem || prevIlvl == null || prevNumId == null) { |
| 61 | + const env: Environment = { |
| 62 | + type: 'environment', |
| 63 | + env: 'enumerate', |
| 64 | + content: listItem, |
| 65 | + _renderInfo: { |
| 66 | + ilvl, |
| 67 | + numId, |
| 68 | + }, |
| 69 | + } |
| 70 | + |
| 71 | + acc.push(env) |
| 72 | + return acc |
| 73 | + } |
| 74 | + |
| 75 | + // there is a previous list item, and it's the same level, so we need to add it to the previous environment |
| 76 | + if (isPrevListItem && ilvl === prevIlvl && numId === prevNumId) { |
| 77 | + const mainEnv = acc[acc.length - 1] as Environment |
| 78 | + |
| 79 | + if (mainEnv.env !== 'enumerate') { |
| 80 | + throw new Error('prevEnv.env !== enumerate') |
| 81 | + } |
| 82 | + |
| 83 | + const embeddedEnvs = findEmbeddedEnvs(mainEnv) |
| 84 | + const lastEnv = embeddedEnvs[embeddedEnvs.length - 1] |
| 85 | + |
| 86 | + lastEnv.content.push(...listItem) |
| 87 | + return acc |
| 88 | + } |
| 89 | + |
| 90 | + // there is a previous list item, and it's a lower level, so we need to add a new environment inside the previous environment |
| 91 | + if (isPrevListItem && ilvl > prevIlvl) { |
| 92 | + const mainEnv = acc[acc.length - 1] as Environment |
| 93 | + const embeddedEnvs = findEmbeddedEnvs(mainEnv) |
| 94 | + const lastEnv = embeddedEnvs[embeddedEnvs.length - 1] |
| 95 | + const env: Environment = { |
| 96 | + type: 'environment', |
| 97 | + env: 'enumerate', |
| 98 | + content: listItem, |
| 99 | + _renderInfo: { |
| 100 | + ilvl, |
| 101 | + numId, |
| 102 | + }, |
| 103 | + } |
| 104 | + lastEnv.content.push(env) |
| 105 | + return acc |
| 106 | + } |
| 107 | + |
| 108 | + // there is a previous list item, and it's a higher level, so we need to close the previous environment and create a new one |
| 109 | + if (isPrevListItem && ilvl < prevIlvl) { |
| 110 | + const mainEnv = acc[acc.length - 1] as Environment |
| 111 | + const embeddedEnvs = findEmbeddedEnvs(mainEnv) |
| 112 | + const toBeEmbeddedEnv = |
| 113 | + embeddedEnvs[embeddedEnvs.length - (prevIlvl - ilvl) - 1] |
| 114 | + |
| 115 | + const env: Environment = { |
| 116 | + type: 'environment', |
| 117 | + env: 'enumerate', |
| 118 | + content: listItem, |
| 119 | + _renderInfo: { |
| 120 | + ilvl, |
| 121 | + numId, |
| 122 | + }, |
| 123 | + } |
| 124 | + |
| 125 | + if (!toBeEmbeddedEnv) { |
| 126 | + acc.push(env) |
| 127 | + return acc |
| 128 | + } |
| 129 | + |
| 130 | + if (toBeEmbeddedEnv._renderInfo?.ilvl !== ilvl) { |
| 131 | + toBeEmbeddedEnv.content.push(env) |
| 132 | + return acc |
| 133 | + } |
| 134 | + |
| 135 | + const item = makeItem(h, child) |
| 136 | + |
| 137 | + toBeEmbeddedEnv.content.push(...item) |
| 138 | + return acc |
| 139 | + } |
| 140 | + |
| 141 | + // if we are immediately starting a new, but different list after a previous list, we need to close the previous list and start a new one |
| 142 | + if (isPrevListItem && ilvl === prevIlvl && numId !== prevNumId) { |
| 143 | + const mainEnv = acc[acc.length - 1] as Environment |
| 144 | + const embeddedEnvs = findEmbeddedEnvs(mainEnv) |
| 145 | + const lastEnv = embeddedEnvs[embeddedEnvs.length - 1] |
| 146 | + const env: Environment = { |
| 147 | + type: 'environment', |
| 148 | + env: 'enumerate', |
| 149 | + content: listItem, |
| 150 | + _renderInfo: { |
| 151 | + ilvl, |
| 152 | + numId, |
| 153 | + }, |
| 154 | + } |
| 155 | + lastEnv.content.push(env) |
| 156 | + return acc |
| 157 | + } |
| 158 | + |
| 159 | + return acc |
| 160 | + }, [] as UnifiedLatexNode[]) |
| 161 | + return processedBody |
| 162 | +} |
| 163 | + |
| 164 | +function makeItem( |
| 165 | + h: H, |
| 166 | + item: P |
| 167 | +): [typeof PB, Macro, typeof SP, ...UnifiedLatexNode[], typeof PB] { |
| 168 | + const mIte = m('item') |
| 169 | + updateRenderInfo(mIte, { |
| 170 | + hangingIndent: true, |
| 171 | + inParMode: true, |
| 172 | + }) |
| 173 | + |
| 174 | + return [PB, mIte, SP, ...all(h, item), PB] |
| 175 | +} |
| 176 | + |
| 177 | +function findEmbeddedEnvs(envs: Environment[] | Environment): Environment[] { |
| 178 | + envs = Array.isArray(envs) ? envs : [envs] |
| 179 | + |
| 180 | + if (envs.length === 0) return [] |
| 181 | + const env = envs[envs.length - 1] |
| 182 | + if (env.content.length === 0) return envs |
| 183 | + |
| 184 | + const last = env.content[env.content.length - 1] |
| 185 | + |
| 186 | + if (last.type !== 'environment') return envs |
| 187 | + |
| 188 | + return findEmbeddedEnvs([...envs, last]) |
| 189 | +} |
| 190 | + |
| 191 | +function makeOne(h: H, node: Element, body: Body): UnifiedLatexNode[] { |
| 192 | + const res = one(h, node, body) |
| 193 | + if (!res) { |
| 194 | + return [] |
| 195 | + } |
| 196 | + |
| 197 | + if (Array.isArray(res)) { |
| 198 | + return res |
| 199 | + } |
| 200 | + |
| 201 | + return [res] |
10 | 202 | } |
0 commit comments