Commit fcb3f73d authored by Robert Knight's avatar Robert Knight

Improve types in `render-markdown.js` and `markdown-commands.js`

parent 0c010216
......@@ -173,6 +173,10 @@ export function toggleSpanStyle(state, prefix, suffix, placeholder) {
return newState;
}
/**
* @param {string} str
* @param {number} pos
*/
function startOfLine(str, pos) {
const start = str.lastIndexOf('\n', pos);
if (start < 0) {
......@@ -182,6 +186,10 @@ function startOfLine(str, pos) {
}
}
/**
* @param {string} str
* @param {number} pos
*/
function endOfLine(str, pos) {
const end = str.indexOf('\n', pos);
if (end < 0) {
......
......@@ -14,6 +14,7 @@ DOMPurify.addHook('afterSanitizeAttributes', node => {
});
function targetBlank() {
/** @param {string} text */
function filter(text) {
return text.replace(/<a href=/g, '<a target="_blank" href=');
}
......@@ -22,6 +23,7 @@ function targetBlank() {
let converter;
/** @param {string} markdown */
function renderMarkdown(markdown) {
if (!converter) {
// see https://github.com/showdownjs/showdown#valid-options
......@@ -40,17 +42,29 @@ function renderMarkdown(markdown) {
return converter.makeHtml(markdown);
}
/** @param {number} id */
function mathPlaceholder(id) {
return '{math:' + id.toString() + '}';
}
/**
* @typedef MathBlock
* @prop {number} id
* @prop {boolean} inline
* @prop {string} expression
*/
/**
* Parses a string containing mixed markdown and LaTeX in between
* '$$..$$' or '\( ... \)' delimiters and returns an object containing a
* list of math blocks found in the string, plus the input string with math
* blocks replaced by placeholders.
*
* @param {string} content
* @return {{ content: string, mathBlocks: MathBlock[]}}
*/
function extractMath(content) {
/** @type {MathBlock[]} */
const mathBlocks = [];
let pos = 0;
let replacedContent = content;
......@@ -113,6 +127,10 @@ function extractMath(content) {
};
}
/**
* @param {string} html
* @param {MathBlock[]} mathBlocks
*/
function insertMath(html, mathBlocks) {
return mathBlocks.reduce((html, block) => {
let renderedMath;
......@@ -131,6 +149,9 @@ function insertMath(html, mathBlocks) {
}, html);
}
/**
* @param {string} markdown
*/
export function renderMathAndMarkdown(markdown) {
// KaTeX takes care of escaping its input, so we want to avoid passing its
// output through the HTML sanitizer. Therefore we first extract the math
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment