Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
d453e11
Changed Escaping Function
urshofer Nov 9, 2017
d1543f6
Merge pull request #1 from sampathsris/master
urshofer Nov 9, 2017
c1227c4
adding options.levelStyles
urshofer Nov 9, 2017
d1d47a2
Update README.md
urshofer Nov 9, 2017
86b0977
Moved Backslash Up
urshofer Nov 9, 2017
4015f9b
changed br and par
urshofer Dec 18, 2017
933bdb1
moved back
urshofer Dec 18, 2017
c76d854
escape fix
urshofer Jan 19, 2018
021ead7
try to fix backslash trouble
urshofer Jan 19, 2018
a196b61
Adding Space after BR
urshofer Jan 19, 2018
355f536
Fixing some quotes
urshofer Feb 22, 2018
6e57dc6
adding dashes
urshofer Feb 23, 2018
21f1c45
arrow
urshofer Feb 23, 2018
935ea4d
changed to newline
urshofer Feb 23, 2018
eb8f03c
some additional newlines
urshofer Feb 23, 2018
7c18e87
escaping header content
urshofer Feb 23, 2018
714d6c6
escaping
urshofer Feb 23, 2018
cd6fbb7
test
urshofer Feb 23, 2018
7f329c7
test
urshofer Feb 23, 2018
735c15f
Passing RAW into heading
urshofer Feb 23, 2018
5512fca
back to \\
urshofer Feb 23, 2018
f62c86f
escaping verbatim
urshofer Feb 23, 2018
87b9431
escaping verbatim
urshofer Feb 23, 2018
1721b5d
added more escapes
urshofer Feb 24, 2018
e9c8777
adding arrow, fixing global regexp
urshofer Feb 24, 2018
6b2fea4
exporting texEscape
urshofer Mar 2, 2018
d6d0f2a
changed verbatim
urshofer Mar 2, 2018
8dad691
changed verbatim
urshofer Mar 2, 2018
136cee8
changed verbatim again
urshofer Mar 2, 2018
23fa959
switch back to old version
urshofer Mar 2, 2018
dbae19a
adding space instead of non-breaking char
urshofer Mar 5, 2018
96ed518
change new line policy
urshofer Mar 5, 2018
23b338f
adding new line after \\
urshofer Mar 5, 2018
a29c94f
new line fix
urshofer Mar 5, 2018
4980f56
adding leavevmode
urshofer Mar 5, 2018
6da7c8c
different spacing
urshofer Mar 5, 2018
f6f294e
adding space
urshofer Mar 5, 2018
c62cbb3
add space
urshofer Mar 5, 2018
3360894
\- in verbatim for spaces
urshofer Mar 5, 2018
1af89e4
adding shy
urshofer Mar 16, 2018
c283df1
shy fix
urshofer Mar 16, 2018
2639cf7
U001F
urshofer Mar 16, 2018
3414c49
just empty shy
urshofer Mar 16, 2018
4ff85b5
adding dots{}
urshofer Oct 5, 2018
346c16d
Dynamic Implementation for Verbatim Environments
urshofer Oct 17, 2019
ed21fe8
Update index.js
urshofer Oct 17, 2019
4916c6f
Update index.js
urshofer Oct 17, 2019
51bb627
Update index.js
urshofer Nov 28, 2019
0e21812
adding square brackets for headings without newline
urshofer Apr 30, 2021
cd1dd17
add space
urshofer May 4, 2021
6238157
replace all new line occurances
urshofer Jun 10, 2021
d1708b7
grr. regex!
urshofer Jun 10, 2021
e46d81b
split support for header tags
urshofer Nov 17, 2021
fc15312
added quot to htmlUnescape
urshofer Apr 18, 2023
2dd7864
removed ds_ store
urshofer Apr 18, 2023
f146cf3
removed ds_store
urshofer Apr 18, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules/
test/output/
npm-debug.log
npm-debug.log
.DS_Store
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,23 @@ Following are the options for `marked-tex-renderer`:
* `delRenderer`: Renderer function for deleted text. Should have the signature `function (text)`, and should return TeX source for deleted text.
* `linkRenderer`: Renderer function for hyperlinks. Should have the signature `function (href, title, text)`, and should return TeX source for hyperlink.
* `imageRenderer`: Renderer function for images. Should have the signature `function (href, title, text)`, and should return TeX source for image.

* `levelStyles`: Object with LaTeX commands how to translate headings. Default is `{}` and will result in the default usage of headings defined in `Renderer.prototype.heading`. See example for how to change it.

`levelStyles` Example:

This example would set all headings to the same paragraph style:

marked.setOptions({
levelStyles: {
1: '\\paragraph',
2: '\\paragraph',
3: '\\paragraph',
4: '\\paragraph',
5: '\\paragraph',
6: '\\paragraph'
}
});

This would reset the styles to the default setting:

marked.setOptions({levelStyles: {}});
128 changes: 102 additions & 26 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,21 @@ Renderer.prototype.failOnUnsupported = function() {
};

Renderer.prototype.code = function (code, lang, escaped) {
return [
'\\begin{verbatim}',
code,
'\\end{verbatim}'
].join(NEWLINE) + NEWLINE;
if (this.options.verbatimRenderer) {
return this.options.verbatimRenderer(code, lang, escaped);
} else if (this.failOnUnsupported()) {
throw new Error(
'Client should provide a function to render Verbatim Text. ' +
'Use options.verbatimRenderer = function (code, lang, escaped)');
} else {
// some text without an image would be weird. return ''.
return '';
}
};




Renderer.prototype.blockquote = function (quote) {
return [
'\\begin{quote}',
Expand All @@ -43,33 +51,42 @@ Renderer.prototype.html = function (html) {
Renderer.prototype.heading = function (text, level, raw) {
var command = '';

var levelStyles = [];
if (this.options.hasOwnProperty('levelStyles')) {
levelStyles = this.options.levelStyles;
}

switch (level) {
case 1:
command = '\\chapter';
command = levelStyles[1] || '\\chapter';
break;
case 2:
command = '\\section';
command = levelStyles[2] || '\\section';
break;
case 3:
command = '\\subsection';
command = levelStyles[3] || '\\subsection';
break;
case 4:
command = '\\subsubsection';
command = levelStyles[4] || '\\subsubsection';
break;
case 5:
command = '\\paragraph';
command = levelStyles[5] || '\\paragraph';
break;
case 6:
command = '\\subparagraph';
command = levelStyles[6] || '\\subparagraph';
break;
}

if (command !== '' && text.indexOf('\\{-\\}') !== -1) {
command += '*';
text = text.replace(' \\{-\\}', '').replace('\\{-\\}', '');
}

return NEWLINE + command + '{' + text + '}' + NEWLINE;
if (text.indexOf('||') !== -1) {
text = text.split('||');
return NEWLINE + command + '[' + text[1] + ']{' + text[0] + '}' + NEWLINE;
}
else {
return NEWLINE + command + '[' + replaceNewline(text) + ']{' + text + '}' + NEWLINE;
}
};

Renderer.prototype.hr = function () {
Expand Down Expand Up @@ -205,7 +222,7 @@ Renderer.prototype.codespan = function (text) {
};

Renderer.prototype.br = function () {
return '\\\\';
return '\\\\ ';
};

Renderer.prototype.del = function (text) {
Expand Down Expand Up @@ -251,6 +268,18 @@ Renderer.prototype.text = function (text) {
return texEscape(htmlUnescape(text));
};

/*
* Implementation for Standard Verbatim Environment
*/

Renderer.verbatimImpl = function (code, lang, escaped) {
return [
'\\begin{verbatim}',
texEscape(htmlUnescape(code)).replace(/ /g,'\\- '),
'\\end{verbatim}'
].join(NEWLINE) + NEWLINE;
};

/*
* Implementation for unsupported features of plain TeX
*/
Expand All @@ -277,6 +306,25 @@ Renderer.imageImpl = function (herf, title, text) {
].join(NEWLINE) + NEWLINE;
};

/* Export the escape function as well */

Renderer.texEscape = function(text) {
return texEscape(text);
}

/* Export the clear new line function as well */

Renderer.replaceNewline = function(text) {
return replaceNewline(text);
}


/* Export the htmlUnescape function as well */

Renderer.htmlUnescape = function(html) {
return htmlUnescape(html);
}

/*
* Helpers
*/
Expand All @@ -302,6 +350,7 @@ function htmlUnescape(html) {

if (n === 'colon') return ':';
if (n === 'amp') return '&';
if (n === 'quot') return '"';

if (n.charAt(0) === '#') {
var charCode = 0;
Expand All @@ -319,20 +368,47 @@ function htmlUnescape(html) {
});
}

function replaceNewline(text) {
return text.replace(/----force-new-line---/g, ' ');
}

function texEscape(text) {
// some characters have special meaning in TeX
// \ & % $ # _ { } ~ ^
return text
.replace(/\\/g, '\\textbackslash')
.replace(/\&/g, '\\&')
.replace(/%/g, '\\%')
.replace(/\$/g, '\\$')
.replace(/#/g, '\\#')
.replace(/\_/g, '\\_')
.replace(/\{/g, '\\{')
.replace(/\}/g, '\\}')
.replace(/~/g, '\\textasciitilde')
.replace(/\^/g, '\\textasciicircum');
return text
.replace(/\\\\/g, '\n')
.replace(/\\/g, '\\textbackslash ')
.replace(/[^\n](\n\n)[^\n]/g, function(a){return(a.substr(0,1) + '\\par ' + a.substr(-1,1));})
.replace(/\n/g, '\\leavevmode \\\\\n')
.replace(/\{/g, '\\{')
.replace(/\}/g, '\\}')
.replace(/\]/g, '{]}')
.replace(/\[/g, '{[}')
.replace(/\&/g, '\\&')
.replace(/%/g, '\\%')
.replace(/\$/g, '\\$')
.replace(/#/g, '\\#')
.replace(/\_/g, '\\_')
.replace(/~/g, '\\textasciitilde{}')
.replace(/\^/g, '\\textasciicircum{}')
.replace(/„/g, '\\quotedblbase{}')
.replace(/“/g, '\\textquotedblleft{}')
.replace(/”/g, '\\textquotedblright{}')
.replace(/“/g, '``')
.replace(/…/g, '\\dots{}')
.replace(/”/g, '\'\'')
.replace(/’/g, '\\textquoteright{}')
.replace(/´/g, '\\textasciiacute{}')
.replace(/"/g, '\\textquotedbl{}')
.replace(/—/g, '\\textemdash{}')
.replace(/–/g, '\\textendash{}')
.replace(/→/g, '\\textrightarrow{}')
.replace(/↑/g, '\\textuparrow{}')
.replace(/↓/g, '\\textdownarrow{}')
.replace(/←/g, '\\textleftarrow{}')
.replace(/\u00AD/g, '')
.replace(/\u001F/g, '');
}


module.exports = Renderer;