-
Notifications
You must be signed in to change notification settings - Fork 792
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(compiler): customize readme mermaid diagram colors (#5980)
* feat(compiler): add docs option to the config Apply colors from the `docs.markdown.targetComponent` to the my-component node in the mermaid diagram. fixes: #2876 * feat(compiler): add docs option to the config Apply colors from the `docs.markdown.targetComponent` to the my-component node in the mermaid diagram. fixes: #2876 * format(): prettier * refactor: refactor after code review, add tests for depsToMarkdown func * feat: move validation logic for config.docs to validateConfig * fix: deps * fix(compiler): build error & logger message --------- Co-authored-by: Tanner Reits <[email protected]>
- Loading branch information
1 parent
2569abd
commit 9ca8951
Showing
11 changed files
with
1,964 additions
and
73 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,13 @@ | ||
import type * as d from '../../declarations'; | ||
|
||
type DefaultTargetComponentConfig = d.Config['docs']['markdown']['targetComponent']; | ||
|
||
export const DEFAULT_DEV_MODE = false; | ||
export const DEFAULT_HASHED_FILENAME_LENGTH = 8; | ||
export const MIN_HASHED_FILENAME_LENGTH = 4; | ||
export const MAX_HASHED_FILENAME_LENGTH = 32; | ||
export const DEFAULT_NAMESPACE = 'App'; | ||
export const DEFAULT_TARGET_COMPONENT_STYLES: DefaultTargetComponentConfig = { | ||
background: '#f9f', | ||
textColor: '#333', | ||
}; |
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
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,42 @@ | ||
import * as d from '../../declarations'; | ||
import { UnvalidatedConfig } from '../../declarations'; | ||
import { isHexColor } from '../docs/readme/docs-util'; | ||
import { DEFAULT_TARGET_COMPONENT_STYLES } from './constants'; | ||
|
||
/** | ||
* Validate the `.docs` property on the supplied config object and | ||
* return a properly-validated value. | ||
* | ||
* @param config the configuration we're examining | ||
* @param logger the logger that will be set on the config | ||
* @returns a suitable/default value for the docs property | ||
*/ | ||
export const validateDocs = (config: UnvalidatedConfig, logger: d.Logger): d.ValidatedConfig['docs'] => { | ||
const { background: defaultBackground, textColor: defaultTextColor } = DEFAULT_TARGET_COMPONENT_STYLES; | ||
|
||
let { background = defaultBackground, textColor = defaultTextColor } = | ||
config.docs?.markdown?.targetComponent ?? DEFAULT_TARGET_COMPONENT_STYLES; | ||
|
||
if (!isHexColor(background)) { | ||
logger.warn( | ||
`'${background}' is not a valid hex color. The default value for diagram backgrounds ('${defaultBackground}') will be used.`, | ||
); | ||
background = defaultBackground; | ||
} | ||
|
||
if (!isHexColor(textColor)) { | ||
logger.warn( | ||
`'${textColor}' is not a valid hex color. The default value for diagram text ('${defaultTextColor}') will be used.`, | ||
); | ||
textColor = defaultTextColor; | ||
} | ||
|
||
return { | ||
markdown: { | ||
targetComponent: { | ||
background, | ||
textColor, | ||
}, | ||
}, | ||
}; | ||
}; |
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
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
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,150 @@ | ||
import type * as d from '../../../declarations'; | ||
import { DEFAULT_TARGET_COMPONENT_STYLES } from '../../config/constants'; | ||
import { depsToMarkdown } from '../readme/markdown-dependencies'; | ||
|
||
describe('depsToMarkdown()', () => { | ||
it('should use default settings if docs.markdown configuration was not provided', () => { | ||
const mockConfig = { | ||
docs: { | ||
markdown: { | ||
targetComponent: { | ||
...DEFAULT_TARGET_COMPONENT_STYLES, | ||
}, | ||
}, | ||
}, | ||
} as d.ValidatedConfig; | ||
const md = depsToMarkdown( | ||
{ | ||
dependencies: [], | ||
dependencyGraph: { | ||
's-test': ['s-test-dep1'], | ||
}, | ||
dependents: [], | ||
docs: '', | ||
docsTags: [], | ||
encapsulation: undefined, | ||
events: [], | ||
listeners: [], | ||
methods: [], | ||
parts: [], | ||
props: [], | ||
readme: '', | ||
slots: [], | ||
styles: [], | ||
tag: '', | ||
usage: undefined, | ||
}, | ||
[], | ||
mockConfig, | ||
); | ||
expect(md).toEqual([ | ||
'## Dependencies', | ||
'', | ||
'### Graph', | ||
'```mermaid', | ||
'graph TD;', | ||
' s-test --> s-test-dep1', | ||
` style fill:${DEFAULT_TARGET_COMPONENT_STYLES.background},stroke:${DEFAULT_TARGET_COMPONENT_STYLES.textColor},stroke-width:4px`, | ||
'```', | ||
'', | ||
]); | ||
}); | ||
|
||
it('should use provided background settings for generated dependencies graph', () => { | ||
const mockColor = '#445334'; | ||
const mockConfig = { | ||
docs: { | ||
markdown: { | ||
targetComponent: { | ||
background: mockColor, | ||
textColor: DEFAULT_TARGET_COMPONENT_STYLES.textColor, | ||
}, | ||
}, | ||
}, | ||
} as d.ValidatedConfig; | ||
const md = depsToMarkdown( | ||
{ | ||
dependencies: [], | ||
dependencyGraph: { | ||
's-test': ['s-test-dep1'], | ||
}, | ||
dependents: [], | ||
docs: '', | ||
docsTags: [], | ||
encapsulation: undefined, | ||
events: [], | ||
listeners: [], | ||
methods: [], | ||
parts: [], | ||
props: [], | ||
readme: '', | ||
slots: [], | ||
styles: [], | ||
tag: '', | ||
usage: undefined, | ||
}, | ||
[], | ||
mockConfig, | ||
); | ||
expect(md).toEqual([ | ||
'## Dependencies', | ||
'', | ||
'### Graph', | ||
'```mermaid', | ||
'graph TD;', | ||
' s-test --> s-test-dep1', | ||
` style fill:${mockColor},stroke:${DEFAULT_TARGET_COMPONENT_STYLES.textColor},stroke-width:4px`, | ||
'```', | ||
'', | ||
]); | ||
}); | ||
|
||
it('should use provided text color settings for generated dependencies graph', () => { | ||
const mockColor = '#445334'; | ||
const mockConfig = { | ||
docs: { | ||
markdown: { | ||
targetComponent: { | ||
background: DEFAULT_TARGET_COMPONENT_STYLES.background, | ||
textColor: mockColor, | ||
}, | ||
}, | ||
}, | ||
} as d.ValidatedConfig; | ||
const md = depsToMarkdown( | ||
{ | ||
dependencies: [], | ||
dependencyGraph: { | ||
's-test': ['s-test-dep1'], | ||
}, | ||
dependents: [], | ||
docs: '', | ||
docsTags: [], | ||
encapsulation: undefined, | ||
events: [], | ||
listeners: [], | ||
methods: [], | ||
parts: [], | ||
props: [], | ||
readme: '', | ||
slots: [], | ||
styles: [], | ||
tag: '', | ||
usage: undefined, | ||
}, | ||
[], | ||
mockConfig, | ||
); | ||
expect(md).toEqual([ | ||
'## Dependencies', | ||
'', | ||
'### Graph', | ||
'```mermaid', | ||
'graph TD;', | ||
' s-test --> s-test-dep1', | ||
` style fill:${DEFAULT_TARGET_COMPONENT_STYLES.background},stroke:${mockColor},stroke-width:4px`, | ||
'```', | ||
'', | ||
]); | ||
}); | ||
}); |
Oops, something went wrong.