-
Notifications
You must be signed in to change notification settings - Fork 70
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 and YAML file
- Loading branch information
1 parent
a1c9f35
commit a793f4d
Showing
37 changed files
with
2,243 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
88 changes: 88 additions & 0 deletions
88
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,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) }, | ||
}, | ||
}, | ||
}, | ||
]; |
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; |
147 changes: 147 additions & 0 deletions
147
packages/threat-composer-app/src/utils/convertToDocx/convertMarkdown/mdast.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,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; |
Oops, something went wrong.