Skip to content

Commit

Permalink
Merge pull request #1017 from mathjax/menu-filters
Browse files Browse the repository at this point in the history
Add ability to not show latex attributes, and reorganize settings menu
  • Loading branch information
dpvc committed Nov 28, 2023
2 parents 9f1fdae + d6aa722 commit c894001
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
26 changes: 16 additions & 10 deletions ts/ui/menu/Menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ const XMLDECLARATION = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>';
* The various values that are stored in the menu
*/
export interface MenuSettings {
filterSRE: boolean;
showSRE: boolean;
showTex: boolean;
texHints: boolean;
semantics: boolean;
zoom: string;
Expand Down Expand Up @@ -123,7 +124,8 @@ export class Menu {
*/
public static OPTIONS: OptionList = {
settings: {
filterSRE: true,
showSRE: false,
showTex: false,
texHints: true,
semantics: false,
zoom: 'NoZoom',
Expand Down Expand Up @@ -462,7 +464,8 @@ export class Menu {
type: 'contextMenu',
id: 'MathJax_Menu',
pool: [
this.variable<boolean>('filterSRE'),
this.variable<boolean>('showSRE'),
this.variable<boolean>('showTex'),
this.variable<boolean>('texHints'),
this.variable<boolean>('semantics'),
this.variable<string> ('zoom'),
Expand Down Expand Up @@ -537,6 +540,13 @@ export class Menu {
this.checkbox('BreakInline', 'Allow In-line Breaks', 'breakInline'),
]),
this.rule(),
this.submenu('MathmlIncludes', 'MathML Includes', [
this.checkbox('showSRE', 'Semantic attributes', 'showSRE'),
this.checkbox('showTex', 'LaTeX attributes', 'showTex'),
this.checkbox('texHints', 'TeX hints', 'texHints'),
this.checkbox('semantics', 'Original as annotation', 'semantics')
]),
this.rule(),
this.submenu('ZoomTrigger', 'Zoom Trigger', [
this.command('ZoomNow', 'Zoom Once Now', () => this.zoom(null, '', this.menu.mathItem)),
this.rule(),
Expand All @@ -556,10 +566,6 @@ export class Menu {
this.rule(),
this.command('Scale', 'Scale All Math...', () => this.scaleAllMath()),
this.rule(),
this.checkbox('filterSRE', 'Filter semantic annotations', 'filterSRE'),
this.checkbox('texHints', 'Add TeX hints to MathML', 'texHints'),
this.checkbox('semantics', 'Add original as annotation', 'semantics'),
this.rule(),
this.command('Reset', 'Reset to defaults', () => this.resetDefaults())
]),
this.submenu('Accessibility', 'Accessibility', [
Expand Down Expand Up @@ -1047,12 +1053,12 @@ export class Menu {
* @returns {string} The serialized version of the internal MathML
*/
protected toMML(math: HTMLMATHITEM): string {
const mml = this.MmlVisitor.visitTree(math.root, math, {
return this.MmlVisitor.visitTree(math.root, math, {
filterSRE: !this.settings.showSRE,
filterTex: !this.settings.showTex,
texHints: this.settings.texHints,
semantics: (this.settings.semantics && math.inputJax.name !== 'MathML')
});
return (!this.settings.filterSRE ? mml :
mml.replace(/ (?:data-semantic-.*?|role|aria-(?:level|posinset|setsize))=".*?"/g, ''));
}

/**
Expand Down
22 changes: 22 additions & 0 deletions ts/ui/menu/MmlVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import {MathItem} from '../../core/MathItem.js';
import {MmlNode} from '../../core/MmlTree/MmlNode.js';
import {PropertyList} from '../../core/Tree/Node.js';
import {SerializedMmlVisitor} from '../../core/MmlTree/SerializedMmlVisitor.js';
import {OptionList, userOptions} from '../../util/Options.js';

Expand All @@ -41,6 +42,8 @@ export class MmlVisitor<N, T, D> extends SerializedMmlVisitor {
* The options controlling the serialization
*/
public options: OptionList = {
filterSRE: true, // True means remove data-semantic, role, and aria attributes
filterTex: true, // True means remove data-latex and data-latexItem attributes
texHints: true, // True means include classes for TeXAtom elements
semantics: false, // True means include original form as annotation in a semantics element
};
Expand Down Expand Up @@ -97,4 +100,23 @@ export class MmlVisitor<N, T, D> extends SerializedMmlVisitor {
+ space + '</math>';
}

/**
* @override
*/
protected getAttributeList(node: MmlNode): PropertyList {
const list = super.getAttributeList(node);
if (this.options.filterTex) {
delete list['data-latex'];
delete list['data-latex-item'];
}
if (this.options.filterSRE) {
const keys = Object.keys(list).filter(
id => id.match(/^(?:data-semantic-.*?|role|aria-(?:level|posinset|setsize))$/)
);
for (const key of keys) {
delete list[key];
}
}
return list;
}
}

0 comments on commit c894001

Please sign in to comment.