-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathstyles.ts
More file actions
75 lines (69 loc) · 2.27 KB
/
styles.ts
File metadata and controls
75 lines (69 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/**
* Paragraph Style
*/
import {
ICharacterStyleOptions, IParagraphStyleOptions, IStylesOptions, UnderlineType
} from 'docx'
import { IMarkdownToken, IMarkdownTheme } from '../types'
import { createMarkdownStyle, markdown } from './markdown'
import { defaultTheme } from './themes'
export function createDefaultStyle(theme: IMarkdownTheme): IStylesOptions['default'] {
return {
document: {
run: {
size: (theme.bodySize ?? defaultTheme.bodySize ?? 12) * 2, // Convert pt to half-points
},
paragraph: {
spacing: {
line: Math.round((theme.lineSpacing ?? defaultTheme.lineSpacing ?? 1.0) * 240), // Convert to twips
lineRule: "auto"
}
}
},
hyperlink: {},
heading1: {},
heading2: {},
heading3: {},
heading4: {},
heading5: {},
heading6: {},
strong: {},
listParagraph: {},
footnoteReference: {},
footnoteText: {},
footnoteTextChar: {},
title: {}
}
}
// Legacy export for backward compatibility: use defaultTheme as input
export const defaultStyle = createDefaultStyle(defaultTheme)
type CreateDocumentStyleOptions = {
theme?: Partial<IMarkdownTheme>
}
export function createDocumentStyle({ theme }: CreateDocumentStyleOptions): IStylesOptions {
const paragraphStyles: IParagraphStyleOptions[] = []
const characterStyles: ICharacterStyleOptions[] = []
const mergedTheme = theme ? { ...defaultTheme, ...theme } : defaultTheme
const markdownTheme = theme ? createMarkdownStyle(mergedTheme) : markdown
const keys = Object.keys(markdownTheme) as IMarkdownToken[]
const styles = { ...createDefaultStyle(mergedTheme) }
for (const key of keys) {
const style = markdownTheme[key]
if (!style) continue
const { className, run, inline, paragraph, basedOn = 'Normal', next = 'Normal', quickFormat = true } = style
if (inline) {
characterStyles.push({ id: className, name: className, basedOn, next, quickFormat, run })
} else {
paragraphStyles.push({ id: className, name: className, basedOn, next, quickFormat, run, paragraph })
}
if (key in styles) {
// @ts-ignore
styles[key] = { ...styles[key], ...style}
}
}
return {
default: styles,
paragraphStyles: paragraphStyles,
characterStyles: characterStyles,
}
}