-
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.
Add component to render item documentation
- Loading branch information
Showing
5 changed files
with
104 additions
and
2 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,27 @@ | ||
<script lang="ts"> | ||
import type { Documentation } from '$lib/kiara.js'; | ||
import sanitizeHtml from 'sanitize-html'; | ||
import { marked } from 'marked'; | ||
export let doc: Documentation; | ||
/** | ||
* Whether to show the full markdown documentation of the item, or just the summary | ||
*/ | ||
export let verbose = true; | ||
marked.use({ | ||
gfm: false | ||
}); | ||
const emptyDocMarker = '-- n/a --'; | ||
</script> | ||
|
||
{#if doc.description && doc.description !== emptyDocMarker} | ||
<p>{doc.description}</p> | ||
{/if} | ||
|
||
{#if verbose && doc.doc} | ||
{#await marked.parse(doc.doc) then html} | ||
{@html sanitizeHtml(html)} | ||
{/await} | ||
{/if} |
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,53 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { Doc } from '../index.js'; | ||
import { render, screen } from '@testing-library/svelte'; | ||
|
||
describe('Doc', () => { | ||
const minimalDoc = { | ||
description: '-- n/a --', | ||
doc: null | ||
}; | ||
|
||
const docWithMarkdown = { | ||
description: 'Create an array of date objects from an array of strings.', | ||
doc: 'This module is very simplistic at the moment, more functionality and options will be added in the future.\n\nAt its core, this module uses the standard parser from the\n[dateutil](https://github.com/dateutil/dateutil) package to parse strings into dates. ' | ||
}; | ||
|
||
const docWithMaliciousMarkdown = { | ||
description: 'Bad', | ||
doc: '<script>alert("hello world")</script>' | ||
}; | ||
it('shows nothing if no meaningful documentation provided', () => { | ||
render(Doc, { props: { doc: minimalDoc } }); | ||
// Don't render the n/a, it isn't useful to the end user | ||
expect(screen.queryByText('-- n/a --')).toBeFalsy(); | ||
}); | ||
|
||
it('shows all available documentation by default', () => { | ||
render(Doc, { props: { doc: docWithMarkdown } }); | ||
// description property is rendered as is | ||
expect(screen.getByText(docWithMarkdown.description)).toBeInTheDocument(); | ||
// all the text from doc is rendered | ||
expect(screen.getByText(/very simplistic/)).toBeInTheDocument(); | ||
}); | ||
|
||
it('only shows summary when verbose is false', () => { | ||
render(Doc, { props: { doc: docWithMarkdown, verbose: false } }); | ||
// description property is rendered as is | ||
expect(screen.getByText(docWithMarkdown.description)).toBeInTheDocument(); | ||
// all the text from doc is rendered | ||
expect(screen.queryByText(/very simplistic/)).not.toBeInTheDocument(); | ||
}); | ||
|
||
it('renders markdown in the doc property', () => { | ||
render(Doc, { props: { doc: docWithMarkdown } }); | ||
expect(screen.getByRole('link', { name: 'dateutil' })).toBeInTheDocument(); | ||
}); | ||
|
||
it('does not render malicious markdown in the doc property', () => { | ||
render(Doc, { props: { doc: docWithMaliciousMarkdown } }); | ||
const theDom = document.body.innerHTML; | ||
expect(theDom).toContain('Bad'); | ||
expect(theDom).not.toContain('script'); | ||
}); | ||
}); |
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 +1 @@ | ||
// Reexport your entry components here | ||
export { default as Doc } from './display/Doc.svelte'; |
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,17 @@ | ||
/** | ||
* Short description of the item | ||
*/ | ||
export type Description = string; | ||
|
||
/** | ||
* Detailed documentation of the item in markdown format | ||
*/ | ||
export type Doc = string | null; | ||
|
||
/** | ||
* Documentation about an item | ||
*/ | ||
export interface Documentation { | ||
description?: Description; | ||
doc?: Doc; | ||
} |