-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3946 from weedySeaDragon/chore/3922_doc-diagram-only
(chore) Docs: add tag to produce only a diagram, not code example
- Loading branch information
Showing
2 changed files
with
183 additions
and
26 deletions.
There are no files selected for viewing
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,115 @@ | ||
import { transformBlocks, transformToBlockQuote } from './docs.mjs'; | ||
|
||
import { remark } from 'remark'; // import it this way so we can mock it | ||
vi.mock('remark'); | ||
|
||
afterEach(() => { | ||
vi.restoreAllMocks(); | ||
}); | ||
|
||
describe('docs.mts', () => { | ||
describe('transformBlocks', () => { | ||
it('uses remark.parse to create the AST for the file ', () => { | ||
const remarkParseSpy = vi | ||
.spyOn(remark, 'parse') | ||
.mockReturnValue({ type: 'root', children: [] }); | ||
const contents = 'Markdown file contents'; | ||
transformBlocks(contents); | ||
expect(remarkParseSpy).toHaveBeenCalledWith(contents); | ||
}); | ||
describe('checks each AST node', () => { | ||
it('does no transformation if there are no code blocks', async () => { | ||
const contents = 'Markdown file contents\n'; | ||
const result = transformBlocks(contents); | ||
expect(result).toEqual(contents); | ||
}); | ||
|
||
describe('is a code block', () => { | ||
const beforeCodeLine = 'test\n'; | ||
const diagram_text = 'graph\n A --> B\n'; | ||
|
||
describe('language = "mermaid-nocode"', () => { | ||
const lang_keyword = 'mermaid-nocode'; | ||
const contents = beforeCodeLine + '```' + lang_keyword + '\n' + diagram_text + '\n```\n'; | ||
|
||
it('changes the language to "mermaid"', () => { | ||
const result = transformBlocks(contents); | ||
expect(result).toEqual( | ||
beforeCodeLine + '\n' + '```' + 'mermaid' + '\n' + diagram_text + '\n```\n' | ||
); | ||
}); | ||
}); | ||
|
||
describe('language = "mermaid" | "mmd" | "mermaid-example"', () => { | ||
const mermaid_keywords = ['mermaid', 'mmd', 'mermaid-example']; | ||
|
||
mermaid_keywords.forEach((lang_keyword) => { | ||
const contents = | ||
beforeCodeLine + '```' + lang_keyword + '\n' + diagram_text + '\n```\n'; | ||
|
||
it('changes the language to "mermaid-example" and adds a copy of the code block with language = "mermaid"', () => { | ||
const result = transformBlocks(contents); | ||
expect(result).toEqual( | ||
beforeCodeLine + | ||
'\n' + | ||
'```mermaid-example\n' + | ||
diagram_text + | ||
'\n```\n' + | ||
'\n```mermaid\n' + | ||
diagram_text + | ||
'\n```\n' | ||
); | ||
}); | ||
}); | ||
}); | ||
|
||
it('calls transformToBlockQuote with the node information', () => { | ||
const lang_keyword = 'note'; | ||
const contents = | ||
beforeCodeLine + '```' + lang_keyword + '\n' + 'This is the text\n' + '```\n'; | ||
|
||
const result = transformBlocks(contents); | ||
expect(result).toEqual(beforeCodeLine + '\n> **Note**\n' + '> This is the text\n'); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('transformToBlockQuote', () => { | ||
// TODO Is there a way to test this with --vitepress given as a process argument? | ||
|
||
describe('vitepress is not given as an argument', () => { | ||
it('everything starts with "> " (= block quote)', () => { | ||
const result = transformToBlockQuote('first line\n\n\nfourth line', 'blorfType'); | ||
expect(result).toMatch(/> (.)*\n> first line(?:\n> ){3}fourth line/); | ||
}); | ||
|
||
it('includes an icon if there is one for the type', () => { | ||
const result = transformToBlockQuote( | ||
'first line\n\n\nfourth line', | ||
'danger', | ||
'Custom Title' | ||
); | ||
expect(result).toMatch(/> \*\*‼️ Custom Title\*\* /); | ||
}); | ||
|
||
describe('a custom title is given', () => { | ||
it('custom title is surrounded in spaces, in bold', () => { | ||
const result = transformToBlockQuote( | ||
'first line\n\n\nfourth line', | ||
'blorfType', | ||
'Custom Title' | ||
); | ||
expect(result).toMatch(/> \*\*Custom Title\*\* /); | ||
}); | ||
}); | ||
|
||
describe.skip('no custom title is given', () => { | ||
it('title is the icon and the capitalized type, in bold', () => { | ||
const result = transformToBlockQuote('first line\n\n\nfourth line', 'blorf type'); | ||
expect(result).toMatch(/> \*\*Blorf Type\*\* /); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |