2021-06-26 22:47:52 +08:00
|
|
|
import { getAttributeOrThrow } from "../lib/attribute";
|
2022-03-15 05:19:56 +08:00
|
|
|
import Markdown from "../lib/markdown";
|
2021-06-26 22:47:52 +08:00
|
|
|
|
|
|
|
/**
|
2022-03-16 18:33:53 +08:00
|
|
|
* A hook used to render Markdown content on the client.
|
2021-06-26 22:47:52 +08:00
|
|
|
*
|
2022-03-16 18:33:53 +08:00
|
|
|
* ## Configuration
|
2021-06-26 22:47:52 +08:00
|
|
|
*
|
2022-03-16 18:33:53 +08:00
|
|
|
* * `data-id` - id of the renderer, under which the content event
|
|
|
|
* is pushed
|
2021-06-26 22:47:52 +08:00
|
|
|
*/
|
|
|
|
const MarkdownRenderer = {
|
|
|
|
mounted() {
|
2022-03-16 18:33:53 +08:00
|
|
|
this.props = this.getProps();
|
2021-06-26 22:47:52 +08:00
|
|
|
|
2022-05-17 09:03:18 +08:00
|
|
|
const markdown = new Markdown(this.el, "", {
|
|
|
|
baseUrl: this.props.sessionPath,
|
|
|
|
});
|
2021-06-26 22:47:52 +08:00
|
|
|
|
|
|
|
this.handleEvent(
|
2022-03-16 18:33:53 +08:00
|
|
|
`markdown_renderer:${this.props.id}:content`,
|
2021-06-26 22:47:52 +08:00
|
|
|
({ content }) => {
|
|
|
|
markdown.setContent(content);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2022-03-16 18:33:53 +08:00
|
|
|
getProps() {
|
|
|
|
return {
|
|
|
|
id: getAttributeOrThrow(this.el, "data-id"),
|
2022-05-17 09:03:18 +08:00
|
|
|
sessionPath: getAttributeOrThrow(this.el, "data-session-path"),
|
2022-03-16 18:33:53 +08:00
|
|
|
};
|
2021-06-26 22:47:52 +08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
export default MarkdownRenderer;
|