Skip to content

Commit

Permalink
feat: Support download the model as Docx and YAML file
Browse files Browse the repository at this point in the history
  • Loading branch information
jessieweiyi committed Jun 7, 2024
1 parent a1c9f35 commit a793f4d
Show file tree
Hide file tree
Showing 37 changed files with 2,243 additions and 89 deletions.
28 changes: 28 additions & 0 deletions packages/threat-composer-app/.projen/deps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/threat-composer-app/.projen/tasks.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions packages/threat-composer-app/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/threat-composer-app/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<head>
<meta charset="utf-8" />
<meta http-equiv='Content-Security-Policy'
content="default-src 'self'; img-src 'self' * data:; font-src 'self' data:; script-src 'self' https://ide-toolkits.threat-composer.aws.dev; style-src 'self' 'unsafe-inline' https://ide-toolkits.threat-composer.aws.dev; media-src 'none' ; frame-src 'none';" />
content="default-src 'self'; img-src 'self' * data: blob:; connect-src 'self' data:; font-src 'self' data:; script-src 'self' https://ide-toolkits.threat-composer.aws.dev; style-src 'self' 'unsafe-inline' https://ide-toolkits.threat-composer.aws.dev; media-src 'none' ; frame-src 'none';" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta name="description" content="threat-composer" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
******************************************************************************************************************** */
import { ThreatModel, ThreatModelView } from '@aws/threat-composer';
import { FC, useCallback, useState } from 'react';
import convertToDocx from '../../utils/convertToDocx';

const ThreatModelReport: FC = () => {
const [isPreview] = useState(() => {
Expand Down Expand Up @@ -45,9 +46,12 @@ const ThreatModelReport: FC = () => {
window.open(`${window.location.pathname}?${urlParams.toString()}`, '_blank', 'noopener,noreferrer,resizable');
}, []);

console.log('Hello');

return (data
? (<ThreatModelView composerMode='Full' data={data} onPrintButtonClick={isPreview ? undefined : handlePrintButtonClick} />)
: (<ThreatModel
convertToDocx={convertToDocx}
isPreview={isPreview}
onPrintButtonClick={isPreview ? undefined : handlePrintButtonClick}
/>));
Expand Down
88 changes: 88 additions & 0 deletions packages/threat-composer-app/src/utils/convertToDocx/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/** *******************************************************************************************************************
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 {
convertInchesToTwip,
ILevelsOptions,
LevelFormat,
AlignmentType,
} from 'docx';

export const ORDERED_LIST_REF = 'ordered';
export const INDENT = 0.5;

export const DEFAULT_NUMBERINGS: ILevelsOptions[] = [
{
level: 0,
format: LevelFormat.DECIMAL,
text: '%1.',
alignment: AlignmentType.START,
},
{
level: 1,
format: LevelFormat.DECIMAL,
text: '%2.',
alignment: AlignmentType.START,
style: {
paragraph: {
indent: { start: convertInchesToTwip(INDENT * 1) },
},
},
},
{
level: 2,
format: LevelFormat.DECIMAL,
text: '%3.',
alignment: AlignmentType.START,
style: {
paragraph: {
indent: { start: convertInchesToTwip(INDENT * 2) },
},
},
},
{
level: 3,
format: LevelFormat.DECIMAL,
text: '%4.',
alignment: AlignmentType.START,
style: {
paragraph: {
indent: { start: convertInchesToTwip(INDENT * 3) },
},
},
},
{
level: 4,
format: LevelFormat.DECIMAL,
text: '%5.',
alignment: AlignmentType.START,
style: {
paragraph: {
indent: { start: convertInchesToTwip(INDENT * 4) },
},
},
},
{
level: 5,
format: LevelFormat.DECIMAL,
text: '%6.',
alignment: AlignmentType.START,
style: {
paragraph: {
indent: { start: convertInchesToTwip(INDENT * 5) },
},
},
},
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/** *******************************************************************************************************************
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 { Paragraph, Table, TableOfContents } from 'docx';
import frontmatter from 'remark-frontmatter';
import gfm from 'remark-gfm';
import markdown from 'remark-parse';
import { unified } from 'unified';
import docx from './plugin';
import fetchImage from '../fetchImage';

const processor = unified()
.use(markdown)
.use(gfm)
.use(frontmatter)
.use(docx,
{
output: 'sections',
imageResolver: fetchImage,
},
);

/**
* Convert the markdown into Docx format
* @param content
*/
const convertMarkdown = async (content: string) => {
const doc = await processor.process(content);
const sections = await doc.result;

return sections as (Paragraph | Table | TableOfContents) [];
};

export default convertMarkdown;
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/** *******************************************************************************************************************
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 type {
Parent,
Literal,
Root,
Paragraph,
Heading,
ThematicBreak,
Blockquote,
List,
ListItem,
Table,
TableRow,
TableCell,
HTML,
Code,
YAML,
Definition,
FootnoteDefinition,
Text,
Emphasis,
Strong,
Delete,
InlineCode,
Break,
Link,
Image,
LinkReference,
ImageReference,
Footnote,
FootnoteReference,
Resource,
Association,
Reference,
Alternative,
} from 'mdast';
export type {
Parent,
Literal,
Root,
Paragraph,
Heading,
ThematicBreak,
Blockquote,
List,
ListItem,
Table,
TableRow,
TableCell,
HTML,
Code,
YAML,
Definition,
FootnoteDefinition,
Text,
Emphasis,
Strong,
Delete,
InlineCode,
Break,
Link,
Image,
LinkReference,
ImageReference,
Footnote,
FootnoteReference,
Resource,
Association,
Reference,
Alternative,
};

export interface TOML extends Literal {
type: 'toml';
}

export interface Math extends Literal {
type: 'math';
}

export interface InlineMath extends Literal {
type: 'inlineMath';
}

export type Content =
| TopLevelContent
| ListContent
| TableContent
| RowContent
| PhrasingContent;

export type TopLevelContent =
| BlockContent
| FrontmatterContent
| DefinitionContent;

export type BlockContent =
| Paragraph
| Heading
| ThematicBreak
| Blockquote
| List
| Table
| HTML
| Code
| Math;

export type FrontmatterContent = YAML | TOML;

export type DefinitionContent = Definition | FootnoteDefinition;

export type ListContent = ListItem;

export type TableContent = TableRow;

export type RowContent = TableCell;

export type PhrasingContent = StaticPhrasingContent | Link | LinkReference;

export type StaticPhrasingContent =
| Text
| Emphasis
| Strong
| Delete
| HTML
| InlineCode
| Break
| Image
| ImageReference
| Footnote
| FootnoteReference
| InlineMath;
Loading

0 comments on commit a793f4d

Please sign in to comment.