Commit f74f0338 authored by Filipa Lacerda's avatar Filipa Lacerda

Merge branch 'notebook-katex-fixes' into 'master'

Fixes multi-line math in notebooks

Closes #30900 and #33074

See merge request !11789
parents 04251829 c65a8a6e
...@@ -30,7 +30,7 @@ ...@@ -30,7 +30,7 @@
| |
\\s\\$(?!\\$) \\s\\$(?!\\$)
) )
(.+?) ((.|\\n)+?)
( (
\\s\\\\end{[a-zA-Z]+}$ \\s\\\\end{[a-zA-Z]+}$
| |
...@@ -45,15 +45,25 @@ ...@@ -45,15 +45,25 @@
let inline = false; let inline = false;
if (typeof katex !== 'undefined') { if (typeof katex !== 'undefined') {
const katexString = text.replace(/\\/g, '\\'); const katexString = text.replace(/&/g, '&')
const matches = new RegExp(katexRegexString, 'gi').exec(katexString); .replace(/&=&/g, '\\space=\\space')
.replace(/<(\/?)em>/g, '_');
const regex = new RegExp(katexRegexString, 'gi');
const matchLocation = katexString.search(regex);
const numberOfMatches = katexString.match(regex);
if (matches && matches.length > 0) { if (numberOfMatches && numberOfMatches.length !== 0) {
if (matches[1].trim() === '$' && matches[3].trim() === '$') { if (matchLocation > 0) {
let matches = regex.exec(katexString);
inline = true; inline = true;
text = `${katexString.replace(matches[0], '')} ${katex.renderToString(matches[2])}`; while (matches !== null) {
const renderedKatex = katex.renderToString(matches[0].replace(/\$/g, ''));
text = `${text.replace(matches[0], ` ${renderedKatex}`)}`;
matches = regex.exec(katexString);
}
} else { } else {
const matches = regex.exec(katexString);
text = katex.renderToString(matches[2]); text = katex.renderToString(matches[2]);
} }
} }
...@@ -79,7 +89,7 @@ ...@@ -79,7 +89,7 @@
}, },
computed: { computed: {
markdown() { markdown() {
return marked(this.cell.source.join('')); return marked(this.cell.source.join('').replace(/\\/g, '\\\\'));
}, },
}, },
}; };
......
...@@ -21,4 +21,10 @@ describe 'Raw files', '(JavaScript fixtures)', type: :controller do ...@@ -21,4 +21,10 @@ describe 'Raw files', '(JavaScript fixtures)', type: :controller do
store_frontend_fixture(blob.data, example.description) store_frontend_fixture(blob.data, example.description)
end end
it 'blob/notebook/math.json' do |example|
blob = project.repository.blob_at('93ee732', 'files/ipython/math.ipynb')
store_frontend_fixture(blob.data, example.description)
end
end end
import Vue from 'vue'; import Vue from 'vue';
import MarkdownComponent from '~/notebook/cells/markdown.vue'; import MarkdownComponent from '~/notebook/cells/markdown.vue';
import katex from 'vendor/katex';
const Component = Vue.extend(MarkdownComponent); const Component = Vue.extend(MarkdownComponent);
window.katex = katex;
describe('Markdown component', () => { describe('Markdown component', () => {
let vm; let vm;
let cell; let cell;
...@@ -38,4 +41,58 @@ describe('Markdown component', () => { ...@@ -38,4 +41,58 @@ describe('Markdown component', () => {
it('renders the markdown HTML', () => { it('renders the markdown HTML', () => {
expect(vm.$el.querySelector('.markdown h1')).not.toBeNull(); expect(vm.$el.querySelector('.markdown h1')).not.toBeNull();
}); });
describe('katex', () => {
beforeEach(() => {
json = getJSONFixture('blob/notebook/math.json');
});
it('renders multi-line katex', (done) => {
vm = new Component({
propsData: {
cell: json.cells[0],
},
}).$mount();
Vue.nextTick(() => {
expect(
vm.$el.querySelector('.katex'),
).not.toBeNull();
done();
});
});
it('renders inline katex', (done) => {
vm = new Component({
propsData: {
cell: json.cells[1],
},
}).$mount();
Vue.nextTick(() => {
expect(
vm.$el.querySelector('p:first-child .katex'),
).not.toBeNull();
done();
});
});
it('renders multiple inline katex', (done) => {
vm = new Component({
propsData: {
cell: json.cells[1],
},
}).$mount();
Vue.nextTick(() => {
expect(
vm.$el.querySelectorAll('p:nth-child(2) .katex').length,
).toBe(4);
done();
});
});
});
}); });
...@@ -40,7 +40,7 @@ module TestEnv ...@@ -40,7 +40,7 @@ module TestEnv
'wip' => 'b9238ee', 'wip' => 'b9238ee',
'csv' => '3dd0896', 'csv' => '3dd0896',
'v1.1.0' => 'b83d6e3', 'v1.1.0' => 'b83d6e3',
'add-ipython-files' => '6d85bb6', 'add-ipython-files' => '93ee732',
'add-pdf-file' => 'e774ebd' 'add-pdf-file' => 'e774ebd'
}.freeze }.freeze
......
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