Skip to content

Commit

Permalink
chore: Style the output docx
Browse files Browse the repository at this point in the history
  • Loading branch information
jessieweiyi committed Jun 7, 2024
1 parent d81f7e8 commit 36ef6f4
Show file tree
Hide file tree
Showing 15 changed files with 248 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/** *******************************************************************************************************************
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License").
You may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
******************************************************************************************************************** */
import { Table as DocxTable, ITableOptions } from 'docx';

class Table extends DocxTable {
constructor(opts: ITableOptions) {
super({
...opts,
margins: {
top: 16,
bottom: 16,
left: 16,
right: 16,
},
});
}
}

export default Table;
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/** *******************************************************************************************************************
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License").
You may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
******************************************************************************************************************** */
import { TableCell, Paragraph, IParagraphOptions } from 'docx';

class TableHeaderCell extends TableCell {
constructor(paragraph: string | IParagraphOptions) {
super({
children: [new Paragraph(paragraph)],
verticalAlign: 'center',
shading: {
fill: '000000',
color: 'FFFFFF',
},
});
}
}

export default TableHeaderCell;
13 changes: 12 additions & 1 deletion packages/threat-composer-app/src/utils/convertToDocx/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,15 @@ export const DEFAULT_NUMBERINGS: ILevelsOptions[] = [
},
},
},
];
];

export const PT_BASE = 20;
export const LINE_BASE = 276;
export const SPACING = {
line: LINE_BASE,
after: PT_BASE * 6,
};
export const LIST_PARA_SPACING = {
line: Math.floor(LINE_BASE * 0.9),
after: PT_BASE * 3,
};
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
Document,
Paragraph,
ParagraphChild,
Table,
TableRow,
TableCell,
TableOfContents,
Expand All @@ -37,6 +36,8 @@ import {
import type { IPropertiesOptions } from 'docx/build/file/core-properties';
import type * as mdast from './mdast';
import { invariant } from './utils';
import Table from '../components/Table';
import TableHeaderCell from '../components/TableHeaderCell';

const ORDERED_LIST_REF = 'ordered';
const INDENT = 0.5;
Expand Down Expand Up @@ -339,6 +340,7 @@ const buildParagraph = ({ children }: mdast.Paragraph, ctx: Context) => {
}
return new Paragraph({
children: nodes,
style: 'normalPara',
indent:
ctx.indent > 0
? {
Expand All @@ -352,11 +354,13 @@ const buildParagraph = ({ children }: mdast.Paragraph, ctx: Context) => {
reference: ORDERED_LIST_REF,
level: list.level,
},
style: 'listPara',
}
: {
bullet: {
level: list.level,
},
style: 'listPara',
})),
});
};
Expand Down Expand Up @@ -443,12 +447,36 @@ const buildTable = ({ children, align }: mdast.Table, ctx: Context) => {
});

return new Table({
rows: children.map((r) => {
return buildTableRow(r, ctx, cellAligns);
rows: children.map((r, index) => {
return index === 0 ? buildTableHeaderRow(r, ctx, cellAligns) : buildTableRow(r, ctx, cellAligns);
}),
});
};

const buildTableHeaderRow = (
{ children }: mdast.TableRow,
ctx: Context,
cellAligns: any[] | undefined,
) => {
return new TableRow({
children: children.map((c, i) => {
return buildTableHeaderCell(c, ctx, cellAligns?.[i]);
}),
});
};

const buildTableHeaderCell = (
{ children }: mdast.TableCell,
ctx: Context,
align: any | undefined,
) => {
const { nodes } = convertNodes(children, ctx);
return new TableHeaderCell({
alignment: align,
children: nodes,
});
};

const buildTableRow = (
{ children }: mdast.TableRow,
ctx: Context,
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
limitations under the License.
******************************************************************************************************************** */
import { DataExchangeFormat } from '@aws/threat-composer';
import { Paragraph, HeadingLevel, TextRun, ImageRun } from 'docx';
import { Paragraph, HeadingLevel, TextRun } from 'docx';
import convertMarkdown from './convertMarkdown';
import fetchImage from './fetchImage';
import getImage from './getImage';

const getArchitecture = async (
data: DataExchangeFormat,
Expand Down Expand Up @@ -51,19 +51,8 @@ const getArchitecture = async (
],
}));

const image = await fetchImage(data.architecture.image);

children.push(new Paragraph({
children: [
new ImageRun({
data: image.image,
transformation: {
width: image.width,
height: image.height,
},
}),
],
}));
const image = await getImage(data.architecture.image);
children.push(image);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
limitations under the License.
******************************************************************************************************************** */
import { TemplateThreatStatement, DataExchangeFormat, standardizeNumericId } from '@aws/threat-composer';
import { Paragraph, HeadingLevel, TextRun, Table, TableCell, TableRow } from 'docx';
import { Paragraph, HeadingLevel, TextRun, TableCell, TableRow } from 'docx';
import Table from './components/Table';
import getAnchorLink from './getAnchorLink';
import getBookmark from './getBookmark';
import getHeaderRow from './getHeaderRow';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
limitations under the License.
******************************************************************************************************************** */
import { Assumption, AssumptionLink, DataExchangeFormat, standardizeNumericId } from '@aws/threat-composer';
import { Paragraph, HeadingLevel, TextRun, Table, TableCell, TableRow } from 'docx';
import { Paragraph, HeadingLevel, TextRun, TableCell, TableRow } from 'docx';
import Table from './components/Table';
import getAnchorLink from './getAnchorLink';
import getBookmark from './getBookmark';
import getHeaderRow from './getHeaderRow';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
limitations under the License.
******************************************************************************************************************** */
import { DataExchangeFormat } from '@aws/threat-composer';
import { Paragraph, HeadingLevel, TextRun, ImageRun } from 'docx';
import { Paragraph, HeadingLevel, TextRun } from 'docx';
import convertMarkdown from './convertMarkdown';
import fetchImage from './fetchImage';
import getImage from './getImage';

const getDataflow = async (
data: DataExchangeFormat,
Expand Down Expand Up @@ -51,19 +51,8 @@ const getDataflow = async (
],
}));

const image = await fetchImage(data.dataflow.image);

children.push(new Paragraph({
children: [
new ImageRun({
data: image.image,
transformation: {
width: image.width,
height: image.height,
},
}),
],
}));
const image = await getImage(data.dataflow.image);
children.push(image);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@
See the License for the specific language governing permissions and
limitations under the License.
******************************************************************************************************************** */
import { Paragraph, TableCell, TableRow } from 'docx';
import { TableRow } from 'docx';
import TableHeaderCell from './components/TableHeaderCell';

const getHeaderRow = (headers: string[]) => {
return new TableRow({
children: headers.map(h => new TableCell({
children: [new Paragraph(h)],
})),
children: headers.map(h => new TableHeaderCell(h)),
});
};

Expand Down
54 changes: 54 additions & 0 deletions packages/threat-composer-app/src/utils/convertToDocx/getImage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/** *******************************************************************************************************************
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License").
You may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
******************************************************************************************************************** */
import { ExternalHyperlink, ImageRun, Paragraph } from 'docx';
import fetchImage from './fetchImage';

const getImage = async (imageUrl: string) => {
const image = await fetchImage(imageUrl);

if (imageUrl.startsWith('https://') || imageUrl.startsWith('http://')) {
return new Paragraph({
children: [
new ExternalHyperlink({
link: imageUrl,
children: [
new ImageRun({
data: image.image,
transformation: {
width: image.width,
height: image.height,
},
}),
],
}),
],
});
}

return new Paragraph({
children: [
new ImageRun({
data: image.image,
transformation: {
width: image.width,
height: image.height,
},
}),
],
});
};

export default getImage;
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
limitations under the License.
******************************************************************************************************************** */
import { Mitigation, MitigationLink, AssumptionLink, DataExchangeFormat, standardizeNumericId } from '@aws/threat-composer';
import { Paragraph, HeadingLevel, TextRun, Table, TableCell, TableRow } from 'docx';
import { Paragraph, HeadingLevel, TextRun, TableCell, TableRow } from 'docx';
import Table from './components/Table';
import getAnchorLink from './getAnchorLink';
import getBookmark from './getBookmark';
import getHeaderRow from './getHeaderRow';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
limitations under the License.
******************************************************************************************************************** */
import { AssumptionLink, DataExchangeFormat, MitigationLink, TemplateThreatStatement, standardizeNumericId } from '@aws/threat-composer';
import { Paragraph, HeadingLevel, TextRun, Table, TableRow, TableCell } from 'docx';
import { Paragraph, HeadingLevel, TextRun, TableRow, TableCell } from 'docx';
import Table from './components/Table';
import getAnchorLink from './getAnchorLink';
import getBookmark from './getBookmark';
import getHeaderRow from './getHeaderRow';
Expand Down
Loading

0 comments on commit 36ef6f4

Please sign in to comment.