Skip to content

Commit

Permalink
feat(ooxast-util-markup-to-style): actually traverse trees lmao
Browse files Browse the repository at this point in the history
  • Loading branch information
tefkah committed Mar 29, 2023
1 parent 17dd780 commit 954233a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { describe, it, expect } from 'vitest'
import { markupToStyle } from './ooxast-util-markup-to-style.js'
import { x } from 'xastscript'
import { P, R, RPr, RPrMap, T } from 'ooxast'
import { P, PPr, R, RPr, RPrMap, T } from 'ooxast'
import { Element } from 'xast'

const makeP = (els: (keyof RPrMap | (keyof RPrMap)[] | Element)[]) =>
x('w:p', [
x('w:pPr'),
x('w:pPr', [x('w:pStyle', { 'w:val': 'Normal' })]),
...els.flatMap((el) =>
Array.isArray(el) || typeof el === 'string'
? (x('w:r', [
Expand Down Expand Up @@ -133,5 +133,23 @@ describe('ooxast-util-markup-to-style', () => {
expect(result).toEqual(p)
})

it('should overwrite any existing styles', () => {
const p = makeP(['b'])
const result = markupToStyle(p, [
{
markup: 'w:b',
style: 'Heading 1',
},
])
console.dir(result, { depth: null })
expect(result).toEqual(
x('w:p', [
x('w:pPr', [x('w:pStyle', { 'w:val': 'Heading 1' })]),

x('w:r', [x('w:rPr', [x('w:b')]), x('w:t', 'Some text with b styling')]),
]),
)
})

it.todo('should work in a realistic enviroment')
})
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { P, PPr, R, RPr, RPrMap, Root, StringTag, Style } from 'ooxast'
import { visit, SKIP } from 'unist-util-visit'
import { visit, SKIP, CONTINUE } from 'unist-util-visit'
import { convertElement, isElement } from 'xast-util-is-element'
import { RPrJSON, getRStyle } from 'ooxast-util-get-style'
import { select, selectAll } from 'xast-util-select'
Expand Down Expand Up @@ -96,9 +96,10 @@ export function markupToStyle<I extends Root | P = Root>(
// visit all paragraphs
// check if the conditions set in options are met
// if so, add the style to the paragraph

visit(tree as any, (node) => {
if (!convertElement<P>('w:p')(node)) {
return SKIP
return CONTINUE
}

let style: string | undefined = undefined
Expand Down

0 comments on commit 954233a

Please sign in to comment.