Skip to content

Commit 1d8668a

Browse files
committed
ignore tex titles for filename
1 parent 7cf0e1f commit 1d8668a

3 files changed

Lines changed: 30 additions & 2 deletions

File tree

src/lib/svg_text_utils.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,20 @@ var LINE_SPACING = require('../constants/alignment').LINE_SPACING;
1313

1414
var FIND_TEX = /([^$]*)([$]+[^$]*[$]+)([^$]*)/;
1515

16+
/**
17+
* Checks whether the given string contains LaTeX markup
18+
* (delimited by a pair of $ signs) and returns the match array if so.
19+
*
20+
* @param {string} str: the string to check for tex
21+
* @return {?Array} the regex match array (truthy) if the string contains tex,
22+
* otherwise null (for an empty/missing string or when no tex delimiters are found).
23+
*/
24+
function matchTex(str) {
25+
if (!str) return null;
26+
return str.match(FIND_TEX);
27+
};
28+
exports.matchTex = matchTex;
29+
1630
exports.convertToTspans = function(_context, gd, _callback) {
1731
var str = _context.text();
1832

@@ -21,7 +35,7 @@ exports.convertToTspans = function(_context, gd, _callback) {
2135
var tex = (!_context.attr('data-notex')) &&
2236
gd && gd._context.typesetMath &&
2337
(typeof MathJax !== 'undefined') &&
24-
str.match(FIND_TEX);
38+
matchTex(str);
2539

2640
var parent = d3.select(_context.node().parentNode);
2741
if(parent.empty()) return;

src/snapshot/download.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
var Lib = require('../lib');
4+
var svgTextUtils = require('../lib/svg_text_utils');
45

56
var toImage = require('../plot_api/to_image');
67

@@ -37,7 +38,12 @@ function downloadImage(gd, opts) {
3738

3839
var potentialFilename = opts.filename || gd.fn;
3940
if (!potentialFilename) {
40-
potentialFilename = Lib.slugify(helpers.getPlotTitle(gd), 40);
41+
const plotTitle = helpers.getPlotTitle(gd);
42+
// Trying to slugify a LaTeX string can result in weird ugly filenames,
43+
// so ignore the title entirely if it contains LaTeX markup
44+
if (!svgTextUtils.matchTex(plotTitle)) {
45+
potentialFilename = Lib.slugify(plotTitle, 40);
46+
}
4147
}
4248

4349
var filename = potentialFilename || 'plot-image';

test/jasmine/tests/download_test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,14 @@ describe('Plotly.downloadImage', function() {
146146
})
147147
.then(done, done.fail);
148148
}, LONG_TIMEOUT_INTERVAL);
149+
150+
it('ignores title if it contains LaTeX markup', function(done) {
151+
downloadDefault({title: {text: '$\\alpha$ + $\\beta$'}})
152+
.then(function(filename) {
153+
expect(filename).toBe('plot-image.png');
154+
})
155+
.then(done, done.fail);
156+
}, LONG_TIMEOUT_INTERVAL);
149157
});
150158
});
151159

0 commit comments

Comments
 (0)