Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
coopwire-hypothesis
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
孙灵跃 Leon Sun
coopwire-hypothesis
Commits
fcb3f73d
Commit
fcb3f73d
authored
Nov 08, 2021
by
Robert Knight
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improve types in `render-markdown.js` and `markdown-commands.js`
parent
0c010216
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
29 additions
and
0 deletions
+29
-0
markdown-commands.js
src/sidebar/markdown-commands.js
+8
-0
render-markdown.js
src/sidebar/render-markdown.js
+21
-0
No files found.
src/sidebar/markdown-commands.js
View file @
fcb3f73d
...
...
@@ -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
)
{
...
...
src/sidebar/render-markdown.js
View file @
fcb3f73d
...
...
@@ -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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment