Skip to content

Commit

Permalink
Add styles
Browse files Browse the repository at this point in the history
  • Loading branch information
Toliak committed Mar 21, 2023
1 parent 4b53459 commit 8698043
Show file tree
Hide file tree
Showing 4 changed files with 323 additions and 45 deletions.
177 changes: 133 additions & 44 deletions printers/docx/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { ListNode, Node, RawNode } from '@md-to-latex/converter/dist/ast/node';
import * as docx from 'docx';
import { XmlComponent } from 'docx';
import { validateDocxRootNode } from './validation';
import { getDocumentGlobalStyles } from './styles';

const processNode: DocxPrinterVisitor<NodeProcessed | RawNode | Node> =
function (printer, node) {
Expand Down Expand Up @@ -79,66 +80,154 @@ export async function printerResultToBuffer(
console.log(result);

const doc = new docx.Document({
styles: getDocumentGlobalStyles(),
numbering: {
config: printer.wordListRefStore.map<
docx.INumberingOptions['config'][0]
>(n => ({
// TODO: fully prepare the list styles for ordered and unordered list
reference: n.ref,
levels: [
{
level: 0,
format: docx.LevelFormat.UPPER_ROMAN,
text: '0.%1',
alignment: docx.AlignmentType.START,
style: {
paragraph: {
leftTabStop: docx.convertMillimetersToTwip(40),
indent: {
firstLine:
docx.convertMillimetersToTwip(20),
config: [
...printer.wordListRefStore.map<
docx.INumberingOptions['config'][0]
>(n => ({
// TODO: fully prepare the list styles for ordered and unordered list
reference: n.ref,
levels: [
{
level: 0,
format: docx.LevelFormat.RUSSIAN_UPPER,
text: '%1)',
alignment: docx.AlignmentType.START,
style: {
paragraph: {
leftTabStop: docx.convertMillimetersToTwip(
15 + 10,
),
indent: {
firstLine:
docx.convertMillimetersToTwip(15),
},
},
},
},
},
{
level: 1,
format: docx.LevelFormat.UPPER_ROMAN,
text: '1.%1',
alignment: docx.AlignmentType.START,
style: {
paragraph: {
leftTabStop: docx.convertMillimetersToTwip(60),
indent: {
firstLine:
docx.convertMillimetersToTwip(40),
{
level: 1,
format: docx.LevelFormat.DECIMAL,
text: '%2)',
alignment: docx.AlignmentType.START,
style: {
paragraph: {
leftTabStop: docx.convertMillimetersToTwip(
15 + 15 + 10,
),
indent: {
firstLine:
docx.convertMillimetersToTwip(
15 + 15,
),
},
},
},
},
},
{
level: 2,
format: docx.LevelFormat.UPPER_ROMAN,
text: '%2.%1',
alignment: docx.AlignmentType.START,
style: {
paragraph: {
leftTabStop: docx.convertMillimetersToTwip(80),
indent: {
firstLine:
docx.convertMillimetersToTwip(60),
{
level: 2,
format: docx.LevelFormat.UPPER_ROMAN,
text: '%3)',
alignment: docx.AlignmentType.START,
style: {
paragraph: {
leftTabStop: docx.convertMillimetersToTwip(
15 + 15 + 15 + 10,
),
indent: {
firstLine:
docx.convertMillimetersToTwip(
15 + 15 + 15,
),
},
},
},
},
},
],
})),
],
})),
{
reference: 'heading-ref',
levels: [
{
level: 0,
format: docx.LevelFormat.DECIMAL,
text: '%1',
alignment: docx.AlignmentType.START,
style: {
paragraph: {
leftTabStop:
docx.convertMillimetersToTwip(10),
},
},
},
{
level: 1,
format: docx.LevelFormat.DECIMAL,
text: '%1.%2',
alignment: docx.AlignmentType.START,
style: {
paragraph: {
leftTabStop: docx.convertMillimetersToTwip(
20 + 15,
),
},
},
},
{
level: 2,
format: docx.LevelFormat.DECIMAL,
text: '%1.%2.%3',
alignment: docx.AlignmentType.START,
style: {
paragraph: {
leftTabStop: docx.convertMillimetersToTwip(
20 + 15,
),
},
},
},
],
},
],
},
features: {
updateFields: true,
},
sections: [
{
properties: {
page: {
size: {
orientation: docx.PageOrientation.PORTRAIT,
height: docx.convertMillimetersToTwip(297),
width: docx.convertMillimetersToTwip(210),
},
margin: {
top: docx.convertMillimetersToTwip(20),
right: docx.convertMillimetersToTwip(10),
bottom: docx.convertMillimetersToTwip(20),
left: docx.convertMillimetersToTwip(30),
},
},
},
footers: {
default: new docx.Footer({
children: [
new docx.Paragraph({
indent: {
firstLine: 0,
},
alignment: docx.AlignmentType.CENTER,
children: [
new docx.TextRun({
children: [docx.PageNumber.CURRENT],
}),
],
}),
],
}),
},
children: [
...result.map(n => {
if (
Expand Down
4 changes: 4 additions & 0 deletions printers/docx/src/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ export async function printFormulaProcessedNode(
await formulaNodeToPicture(node.text),
],
alignment: AlignmentType.CENTER,
style: 'formula-table-cell',
}),
],
verticalAlign: VerticalAlign.CENTER,
Expand All @@ -161,6 +162,7 @@ export async function printFormulaProcessedNode(
new docx.Paragraph({
text: `(${node.index + 1})`,
alignment: AlignmentType.RIGHT,
style: 'formula-table-cell-number',
}),
],
verticalAlign: VerticalAlign.CENTER,
Expand Down Expand Up @@ -239,13 +241,15 @@ export function getWordTable(info: WordTableInfo): PrinterFunctionResult {
}),
...info.tableTitle,
],
style: 'table-caption',
}),
new docx.Table({
width: {
type: WidthType.PERCENTAGE,
size: 100,
},
rows: [info.header, ...info.content],
style: 'table',
}),
],
diagnostic: [],
Expand Down
Loading

0 comments on commit 8698043

Please sign in to comment.