Skip to content

Commit c2c6711

Browse files
committed
add beginnings of word=>unified-latex converter
1 parent f0fbd40 commit c2c6711

File tree

31 files changed

+716
-583
lines changed

31 files changed

+716
-583
lines changed
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2-
"name": "ooxast-util-to-unified-latex"
2+
"name": "ooxast-util-to-unified-latex",
3+
"type": "module"
34
}

libs/ooxast/ooxast-util-to-unified-latex/project.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@
3737
"options": {
3838
"access": "public"
3939
}
40+
},
41+
"test": {
42+
"executor": "@nrwl/jest:jest",
43+
"outputs": ["coverage/libs/ooxast/ooxast-util-to-unified-latex"],
44+
"options": {
45+
"jestConfig": "libs/ooxast/ooxast-util-to-unified-latex/jest.config.js",
46+
"passWithNoTests": true
47+
}
4048
}
4149
},
4250
"tags": []

libs/ooxast/ooxast-util-to-unified-latex/src/lib/all.ts

Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,20 @@
11
import { one } from './one'
2-
import { J, UnifiedLatexContent, Node, Parent, Handle, UnifiedLatexParent } from './types'
2+
import { H, UnifiedLatexNode, Node, Parent, Handle } from './types'
33

44
/**
55
* Convert all nodes in tree using j
66
* @param j ooxast constructor function
77
* @param parent
88
* @returns
99
*/
10-
11-
export function all(
12-
//<T extends UnifiedLatexContent = UnifiedLatexContent>
13-
j: J,
14-
parent: Node
15-
): // Array<T extends UnifiedLatexParent ? T['children'][number] : UnifiedLatexContent>
16-
UnifiedLatexContent[] {
17-
// @ts-expect-error Assume `parent` is a parent.
18-
const nodes: Array<Node> = parent.children || []
19-
const values: Array<UnifiedLatexContent> = []
10+
export function all(h: H, parent: Node | Parent): UnifiedLatexNode[] {
11+
const nodes: Array<Node> = 'children' in parent ? parent.children || [] : []
12+
const values: Array<UnifiedLatexNode> = []
2013
let index = -1
2114
const length = nodes.length
22-
// const child = nodes[index + 1]
23-
24-
// Trim initial and final `<br>`s.
25-
// They’re not semantic per HTML, and they can’t be made in markdown things
26-
// like paragraphs or headings.
27-
// while (child && child.type === 'element' && child.name === 'br') {
28-
// index++
29-
// child = nodes[index + 1]
30-
// }
31-
32-
// child = nodes[length - 1]
33-
34-
// while (
35-
// length - 1 > index &&
36-
// child &&
37-
// child.type === 'element' &&
38-
// child.name === 'br'
39-
// ) {
40-
// length--
41-
// child = nodes[length - 1]
42-
// }
43-
4415
while (++index < length) {
45-
// @ts-expect-error assume `parent` is a parent.
46-
const result = one(j, nodes[index], parent)
16+
const result =
17+
'children' in parent ? one(h, nodes[index], parent) : one(h, nodes[index])
4718

4819
if (Array.isArray(result)) {
4920
values.push(...result)

libs/ooxast/ooxast-util-to-unified-latex/src/lib/handlers/body.spec.ts

Lines changed: 0 additions & 33 deletions
This file was deleted.
Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
// based on https://github.com/syntax-tree/hast-util-to-mdast/blob/main/lib/handlers/em
22

3-
import { P } from 'ooxast'
4-
import { J, Node, Root, Body } from '../types'
5-
import { convertElement } from 'xast-util-is-element'
6-
import { wrapSections } from '../util/wrap-section'
3+
import { H, Body, Handle } from '../types'
74
import { all } from '../all'
85

9-
export function body(j: J, body: Body) {
10-
const bod = all(j, body)
6+
export const body: Handle = (h: H, body: Body) => {
7+
const bod = all(h, body)
118

12-
return wrapSections(j, bod as any)
9+
return bod
1310
}

libs/ooxast/ooxast-util-to-unified-latex/src/lib/handlers/citation.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { J } from '../types'
1+
import { H } from '../types'
22
import { Parent, T } from 'ooxast'
33
import { Data as CSL } from 'csl-json'
44
import { CitationItem, MendeleyCitationItem } from 'ooxast-util-citations'
55

6-
export function citation(j: J, citation: T, parent: Parent) {
6+
export function citation(h: H, citation: T, parent: Parent) {
77
// i const t = select('', citation) as T
88
// if (!t) return
99
if (!citation || !citation?.children?.length) return
@@ -124,7 +124,7 @@ export function cslCitation(text: string) {
124124
//
125125
}
126126

127-
function generateAuthYearFromCSL(j: J, csl: CSL): string {
127+
function generateAuthYearFromCSL(h: H, csl: CSL): string {
128128
// by default Mendeley generates "ITEM-X" ids, which are bad
129129
if (csl?.id && typeof csl.id === 'string' && !`${csl?.id}`?.match('ITEM')) {
130130
return makeUniqueSuffix(j, csl.id, csl)
@@ -138,7 +138,7 @@ function generateAuthYearFromCSL(j: J, csl: CSL): string {
138138
csl
139139
)
140140
}
141-
function makeUniqueSuffix(j: J, key: string, data: CSL) {
141+
function makeUniqueSuffix(h: H, key: string, data: CSL) {
142142
while (j.citeKeys[key] && j.citeKeys[key] !== data.title) {
143143
key = incrementSuffix(key)
144144
}
Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
11
import { all } from '../all'
2-
import { J, Node, Root } from '../types'
3-
import { x } from 'xastscript'
4-
import { Article, Front, Body, Back } from 'unified-latex-types'
2+
import { H } from '../types'
53
import { Document } from 'ooxast'
64
import { select } from 'xast-util-select'
75

8-
export function document(j: J, node: Document): Article {
6+
export function document(h: H, node: Document) {
97
const footnotes = select('w\\:footnotes', node)
10-
const fngroup = footnotes ? [j(node, 'fnGroup', {}, all(j, footnotes))] : []
8+
const fngroup = footnotes ? [h(node, 'fnGroup', {}, all(h, footnotes))] : []
119

12-
return x('article', [
13-
x('front') as Front,
14-
// @ts-expect-error: hush.
15-
all(j, node),
16-
x('back', fngroup) as Back,
17-
]) as Article
10+
return all(h, node)
1811
}

libs/ooxast/ooxast-util-to-unified-latex/src/lib/handlers/drawing.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { all } from '../all'
2-
import { J, Node, Root } from '../types'
2+
import { H, Node, Root } from '../types'
33
import { x } from 'xastscript'
44
import { Article, Front, Body, Back, Fig } from 'unified-latex-types'
55
import { Drawing } from 'ooxast'
66
import { select } from 'xast-util-select'
77

8-
export function drawing(j: J, node: Drawing): Fig {
8+
export function drawing(h: H, node: Drawing): Fig {
99
const blip = select('a\\:blip', node)
1010
if (!blip) return x('fig') as Fig
1111
const ref = blip?.attributes?.['r:embed']

libs/ooxast/ooxast-util-to-unified-latex/src/lib/handlers/footnote.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { J, Element } from '../types'
1+
import { H, Element } from '../types'
22
import { all } from '../all'
33

4-
export function footnote(j: J, node: Element) {
4+
export function footnote(h: H, node: Element) {
55
if (node?.attributes?.type === 'separator') return
66
if (parseInt(node?.attributes?.['w:id'] || '0') < 1) return
77
return j(node, 'fn', node.attributes, all(j, node))
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Footnotes } from 'ooxast'
2-
import { J } from '../types'
2+
import { H } from '../types'
33
import { all } from '../all'
44

5-
export function footnotes(j: J, node: Footnotes) {
5+
export function footnotes(h: H, node: Footnotes) {
66
j(node, 'fnGroup', all(j, node))
77
}

0 commit comments

Comments
 (0)