Skip to content

Commit

Permalink
build release 1.1.27
Browse files Browse the repository at this point in the history
  • Loading branch information
daviestar committed Nov 26, 2021
1 parent d69ee58 commit 401ad1b
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 175 deletions.
22 changes: 1 addition & 21 deletions compiler/src/mdast/__test__/escaping.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ describe('escaping', () => {

it('should display sample rmarkdown syntax', async () => {
// https://bookdown.org/yihui/rmarkdown-cookbook/verbatim-code-chunks.html
// https://markdownmonster.west-wind.com/docs/_5eg1brc0z.htm
const { html } = await testProcessor(`
~~~
\`\`\`{r #chunk options go here}\`r ''\`
Expand Down Expand Up @@ -61,28 +62,7 @@ describe('escaping', () => {
expect(ignoreWhitespace(html)).toBe(expected);
});

it('should display sample html comments', async () => {
// https://www.jamestharpe.com/markdown-comments
const { html } = await testProcessor(`
\`\`\`
<!---begin.rcode #chunk-options
# R code goes here
end.rcode-->
\`\`\`
`);

const expected = ignoreWhitespace(`
<div class="code-wrapper">
<pre><code>&#x3C;!--begin.rcode #chunk-options
# R code goes here
end.rcode--></code></pre>
</div>
`);
expect(ignoreWhitespace(html)).toBe(expected);
});

it('should escape sample latex', async () => {
// https://www.jamestharpe.com/markdown-comments
const { html } = await testProcessor(`
<div class="mathjax-ignore">
\`\`\`latex
Expand Down
2 changes: 1 addition & 1 deletion compiler/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ module.exports = {
// },
// path.join(__dirname, 'package.json')
// ),
new InlineEnvironmentVariablesPlugin({ VERSION: '1.1.26' }),
new InlineEnvironmentVariablesPlugin({ VERSION: '1.1.27' }),
new CopyPlugin({
patterns: [
{ from: './src/knitr/knitr.R', to: './' },
Expand Down
166 changes: 90 additions & 76 deletions release/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -1398,10 +1398,10 @@ function reportErrors(response, file) {
async function formatResponse(response) {
let md = response;
md = removeCustomPythonBinNotice(md);
md = removeHashSigns(md);
md = addCodeBlockClasses(md);
md = removeEmptyLog(md);
md = addErrorCodeBlock(md);
md = removeHashSigns(md);
md = removeEmptyLog(md);
md = addNewLineAfterKable(md);
return md;
}
Expand All @@ -1410,15 +1410,11 @@ function removeCustomPythonBinNotice(md) {
return md.replace(/^\$python\s\[1\]\s"\S+"/, '');
}

function removeHashSigns(md) {
let insideCodeBlock = false;
function addCodeBlockClasses(md) {
return md.split('\n').reduce((acc, line) => {
if (line.startsWith('```')) {
insideCodeBlock = !insideCodeBlock;
}

if (insideCodeBlock) {
acc.push(line.replace(/^##\s+/, ''));
if (line.startsWith('```{.knitr-output}')) {
const lang = findLanguageForOutput(acc);
acc.push(`\`\`\`{.${lang}-output}`);
} else {
acc.push(line);
}
Expand All @@ -1427,11 +1423,17 @@ function removeHashSigns(md) {
}, []).join('\n');
}

function addCodeBlockClasses(md) {
function removeHashSigns(md) {
let insideCodeResponse = false;
let openingLine = '';
return md.split('\n').reduce((acc, line) => {
if (line.startsWith('```{.knitr-output}')) {
const lang = findLanguageForOutput(acc);
acc.push(`\`\`\`{.${lang}-output}`);
if (line.startsWith('```')) {
insideCodeResponse = !insideCodeResponse;
openingLine = insideCodeResponse ? line : '';
}

if (insideCodeResponse && openingLine.endsWith('-output}')) {
acc.push(line.replace(/^##\s+/, ''));
} else {
acc.push(line);
}
Expand All @@ -1446,9 +1448,9 @@ function removeEmptyLog(md) {

function addErrorCodeBlock(md) {
return md.split('\n').reduce((acc, line, idx) => {
if (line.startsWith('Error') && acc[idx - 1].startsWith('```')) {
if (line.startsWith('## Error') && acc[idx - 1].startsWith('```')) {
const lang = findLanguageForOutput(acc.slice(0, -1));
acc[acc.length - 1] = `\`\`\`{.${lang}-error}`;
acc[acc.length - 1] = `\`\`\`{.${lang}-error-output}`;
}

acc.push(line);
Expand Down Expand Up @@ -1570,95 +1572,102 @@ function tex_to_directive_defineProperty(obj, key, value) { if (key in obj) { Ob

function texToAliasDirective(file, ctx) {
// simple regex tests
// why?
assertNoTexTabular(file);
const md = file.contents;
const tex = new tex_js_namespaceObject.TeX({
// Bussproofs requires an output jax
packages: AllPackages_js_namespaceObject.AllPackages.filter(name => name !== 'bussproofs'),
// Allow numbered references
tags: 'ams',
// Allow single $ delimiters
inlineMath: [['$', '$'], ['\\(', '\\)']],
displayMath: [['$$', '$$'], [`\\[`, `\\]`]]
});
const store = buildMmlStore(md, tex); // console.log(store);
const store = buildMmlStore(md);
const result = replaceTexWithPlaceholder(md, store, file); // add store to ctx

const result = replaceTexWithPlaceholder(md, tex, store, file); // add store to ctx

ctx.mmlStore = store; // replace md in VFile
ctx.mmlStore = store.map(o => o.mml); // replace md in VFile

file.contents = postParse(result);
return file;
} // This is based on https://github.com/mathjax/MathJax-demos-node/blob/f70342b69533dbc24b460f6d6ef341dfa7856414/direct/tex2mml-page
// except I don't return the HTML, instead I compile a list of the extracted LaTeX converted to MathML

function buildMmlStore(md, tex) {
function buildMmlStore(md) {
const store = [];
const adaptor = (0,liteAdaptor_js_namespaceObject.liteAdaptor)();
(0,html_js_namespaceObject.RegisterHTMLHandler)(adaptor);
const visitor = new SerializedMmlVisitor_js_namespaceObject.SerializedMmlVisitor();

function storeMml({
math
}) {
for (const item of Array.from(math)) {
// convert to MML
const mml = visitor.visitTree(item.root);
store.push(mml);
const tree = adaptor.parse('**unused**', 'text/html');
item.typesetRoot = adaptor.firstChild(adaptor.body(tree));
}
}

const doc = mathjax_js_namespaceObject.mathjax.document(md, {
InputJax: tex,
InputJax: new tex_js_namespaceObject.TeX({
// Bussproofs requires an output jax
packages: AllPackages_js_namespaceObject.AllPackages.filter(name => name !== 'bussproofs'),
// Allow numbered references
tags: 'ams',
// Allow single $ delimiters
inlineMath: [['$', '$'], ['\\(', '\\)']],
displayMath: [['$$', '$$'], [`\\[`, `\\]`]]
}),
renderActions: {
typeset: [MathItem_namespaceObject.STATE.TYPESET, storeMml]
}
typeset: [MathItem_namespaceObject.STATE.TYPESET, storeMml(adaptor, visitor, store)]
},
// wrap verbatim latex with <div class="mathjax-ignore"></div>
ignoreHtmlClass: 'mathjax-ignore'
});
doc.render();
return store;
}

function replaceTexWithPlaceholder(md, tex, store, file) {
// Extract the LaTeX from the document again
const extractedLatex = tex.findMath([md]); // Replace it with a placeholder for use later
function storeMml(adaptor, visitor, store) {
return ({
math
}) => {
for (const item of Array.from(math)) {
if (item.start.n === undefined) {
throw new Error('start is undefined');
}

if (item.end.n === undefined) {
throw new Error('end is undefined');
}

store.push({
start: item.start.n,
end: item.end.n,
tex: item.math,
display: item.display,
// convert to MML
mml: visitor.visitTree(item.root)
}); // this is only necessary for the MathJax "typeset"
// renderAction to complete without error

return extractedLatex.map((item, idx) => tex_to_directive_objectSpread(tex_to_directive_objectSpread({}, item), {}, {
const tree = adaptor.parse('**unused**', 'text/html');
item.typesetRoot = adaptor.firstChild(adaptor.body(tree));
}
};
}

function replaceTexWithPlaceholder(md, store, file) {
// Replace it with a placeholder for use later
return store.map((item, idx) => tex_to_directive_objectSpread(tex_to_directive_objectSpread({}, item), {}, {
idx
})).reverse().reduce((acc, item) => {
const placeholder = createPlaceholder(item, store, file);
const prev = acc.slice(0, item.start.n);
const next = acc.slice(item.end.n);
const placeholder = createPlaceholder(item, file);
const prev = acc.slice(0, item.start);
const next = acc.slice(item.end);
return prev + placeholder + next;
}, md);
}

function createPlaceholder(item, store, file) {
function createPlaceholder(item, file) {
// escaped dollar sign...
if (item.math === '$') {
if (item.tex === '$') {
return '$';
} // double backslash...


if (item.math === '\\') {
if (item.tex === '\\') {
return '\\\\';
}

const mml = store[item.idx]; // why?

if (!mml) {
return '';
}

assertNoMmlError(mml, file); // debug
// console.log(item.math, mml);
assertNoMmlError(item.mml, file); // debug
// console.log(item);
// reference link...

if (isReferenceLink(item.math)) {
const refNum = extractRefNumFromMml(mml, item.math, file);
const anchor = extractAnchorLinkFromMml(mml, item.math, file);
if (isReferenceLink(item.tex)) {
const refNum = extractRefNumFromMml(item, file);
const anchor = extractAnchorLinkFromMml(item, file);
return `[${refNum}](${anchor})`;
} // normal use case (equation)...

Expand All @@ -1679,7 +1688,10 @@ function isReferenceLink(tex) {
return /^\\ref\{(.+)\}$/.test(tex);
}

function extractRefNumFromMml(mml, tex, file) {
function extractRefNumFromMml({
mml,
tex
}, file) {
const match = mml.match(/<mtext>(.+)<\/mtext>/);

if (match === null) {
Expand All @@ -1694,7 +1706,10 @@ function extractRefNumFromMml(mml, tex, file) {
return match[1];
}

function extractAnchorLinkFromMml(mml, tex, file) {
function extractAnchorLinkFromMml({
mml,
tex
}, file) {
const match = mml.match(/<mrow href="(.+)" class="MathJax_ref">/);

if (match === null) {
Expand Down Expand Up @@ -2186,8 +2201,7 @@ function aliasDirectiveToSvg(ctx) {
case 'blockMath':
{
const idx = getTexIdx(node);
const mml = ctx.mmlStore[idx]; // console.log(mml);

const mml = ctx.mmlStore[idx];
const svg = renderSvg(mml);

const properties = directive_to_svg_objectSpread(directive_to_svg_objectSpread({}, svg.properties), {}, {
Expand Down Expand Up @@ -2324,7 +2338,7 @@ function customCode(node, ctx, file) {
}

function addConsoleHeading(klass) {
if (klass === 'r-output' || klass === 'r-error') {
if (klass === 'r-output' || klass === 'r-error-output') {
return {
type: 'element',
tagName: 'h6',
Expand All @@ -2338,7 +2352,7 @@ function addConsoleHeading(klass) {
};
}

if (klass === 'python-output' || klass === 'python-error') {
if (klass === 'python-output' || klass === 'python-error-output') {
return {
type: 'element',
tagName: 'h6',
Expand Down Expand Up @@ -3114,7 +3128,7 @@ function preParsePhase(file) {
}

function removeCommentedSections(md) {
return md.replace(/<!--[\s\S]*?-->/g, '');
return md.replace(/<!--[^-][\s\S]*?-->/g, '').replace(/<!---/g, '<!--');
}

function escapeDollarsInCodeBlocks(md) {
Expand Down Expand Up @@ -3320,7 +3334,7 @@ async function checkForLatestVersion() {
const response = await external_node_fetch_default()(`https://api.github.com/repos/${repo}/releases/latest`);
const json = await response.json();
const latestTag = json.tag_name.replace('v', '');
const currentVersion = "1.1.26";
const currentVersion = "1.1.27";

if (latestTag !== currentVersion) {
console.log(external_chalk_default().yellow.bold('New version available'));
Expand Down
Loading

0 comments on commit 401ad1b

Please sign in to comment.