forked from awslabs/threat-composer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Support download the model as DOCX file (awslabs#116)
* feat: Support download the model as Docx and YAML file * chore: Remove console log * chore: Style the output docx * chore: Remove commented code
- Loading branch information
1 parent
8e3ed23
commit 24b4b91
Showing
40 changed files
with
2,401 additions
and
89 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
packages/threat-composer-app/src/utils/convertToDocx/components/Table.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
31 changes: 31 additions & 0 deletions
31
packages/threat-composer-app/src/utils/convertToDocx/components/TableHeaderCell.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
99 changes: 99 additions & 0 deletions
99
packages/threat-composer-app/src/utils/convertToDocx/config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/** ******************************************************************************************************************* | ||
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) }, | ||
}, | ||
}, | ||
}, | ||
]; | ||
|
||
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, | ||
}; |
46 changes: 46 additions & 0 deletions
46
packages/threat-composer-app/src/utils/convertToDocx/convertMarkdown/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.