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) { ...@@ -173,6 +173,10 @@ export function toggleSpanStyle(state, prefix, suffix, placeholder) {
return newState; return newState;
} }
/**
* @param {string} str
* @param {number} pos
*/
function startOfLine(str, pos) { function startOfLine(str, pos) {
const start = str.lastIndexOf('\n', pos); const start = str.lastIndexOf('\n', pos);
if (start < 0) { if (start < 0) {
...@@ -182,6 +186,10 @@ function startOfLine(str, pos) { ...@@ -182,6 +186,10 @@ function startOfLine(str, pos) {
} }
} }
/**
* @param {string} str
* @param {number} pos
*/
function endOfLine(str, pos) { function endOfLine(str, pos) {
const end = str.indexOf('\n', pos); const end = str.indexOf('\n', pos);
if (end < 0) { if (end < 0) {
......
...@@ -14,6 +14,7 @@ DOMPurify.addHook('afterSanitizeAttributes', node => { ...@@ -14,6 +14,7 @@ DOMPurify.addHook('afterSanitizeAttributes', node => {
}); });
function targetBlank() { function targetBlank() {
/** @param {string} text */
function filter(text) { function filter(text) {
return text.replace(/<a href=/g, '<a target="_blank" href='); return text.replace(/<a href=/g, '<a target="_blank" href=');
} }
...@@ -22,6 +23,7 @@ function targetBlank() { ...@@ -22,6 +23,7 @@ function targetBlank() {
let converter; let converter;
/** @param {string} markdown */
function renderMarkdown(markdown) { function renderMarkdown(markdown) {
if (!converter) { if (!converter) {
// see https://github.com/showdownjs/showdown#valid-options // see https://github.com/showdownjs/showdown#valid-options
...@@ -40,17 +42,29 @@ function renderMarkdown(markdown) { ...@@ -40,17 +42,29 @@ function renderMarkdown(markdown) {
return converter.makeHtml(markdown); return converter.makeHtml(markdown);
} }
/** @param {number} id */
function mathPlaceholder(id) { function mathPlaceholder(id) {
return '{math:' + id.toString() + '}'; 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 * Parses a string containing mixed markdown and LaTeX in between
* '$$..$$' or '\( ... \)' delimiters and returns an object containing a * '$$..$$' or '\( ... \)' delimiters and returns an object containing a
* list of math blocks found in the string, plus the input string with math * list of math blocks found in the string, plus the input string with math
* blocks replaced by placeholders. * blocks replaced by placeholders.
*
* @param {string} content
* @return {{ content: string, mathBlocks: MathBlock[]}}
*/ */
function extractMath(content) { function extractMath(content) {
/** @type {MathBlock[]} */
const mathBlocks = []; const mathBlocks = [];
let pos = 0; let pos = 0;
let replacedContent = content; let replacedContent = content;
...@@ -113,6 +127,10 @@ function extractMath(content) { ...@@ -113,6 +127,10 @@ function extractMath(content) {
}; };
} }
/**
* @param {string} html
* @param {MathBlock[]} mathBlocks
*/
function insertMath(html, mathBlocks) { function insertMath(html, mathBlocks) {
return mathBlocks.reduce((html, block) => { return mathBlocks.reduce((html, block) => {
let renderedMath; let renderedMath;
...@@ -131,6 +149,9 @@ function insertMath(html, mathBlocks) { ...@@ -131,6 +149,9 @@ function insertMath(html, mathBlocks) {
}, html); }, html);
} }
/**
* @param {string} markdown
*/
export function renderMathAndMarkdown(markdown) { export function renderMathAndMarkdown(markdown) {
// KaTeX takes care of escaping its input, so we want to avoid passing its // 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 // 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