Markdown Support with Marked
Marked is a JavaScript library used to convert Markdown to HTML. You can integrate this library with Survey Creator to let survey authors format question titles, descriptions, and other textual survey content using Markdown syntax. This example shows how to enable Markdown support in Survey Creator.
Access Internal Survey Instances
As Survey Creator is built upon regular surveys from SurveyJS Form Library, you need to access internal survey instances to process Markdown content. Handle SurveyCreatorModel
's onSurveyInstanceCreated
event with a function. The function's options.survey
parameter exposes one of the internal SurveyModel
instances.
Enable Markdown Support
To enable Markdown support, implement a function that handles SurveyModel
's onTextMarkdown
event. The function's options.text
parameter contains a string value with Markdown content. Pass this value to the Marked converter to get HTML markup. Note that the converter wraps the passed Markdown string into an unnecessary paragraph (<p>
tag). Remove this tag and assign the result to the options.html
property.
The Marked library supports HTML tags in the source. However, this feature is considered unsafe because the converter allows any HTML markup to pass through, even if it contains malicious code. To ensure that the resulting HTML markup is safe, it must be processed through a sanitizer. This demo does not use any third-party sanitizer, as SurveyJS includes basic sanitizing capabilities. However, these capabilities do not guarantee 100% protection against malicious code injections. We highly recommend using a dedicated sanitizing library in production code.
Integrate a Rich Text Editor
If you need advanced formatting capabilities (bullet points, numbered lists, hyperlinks, text alignment), you can integrate a rich content editor into Survey Creator. Refer to the following demo for more information: