diff --git a/src/Lexer.js b/src/Lexer.js index da34d3a4..22083629 100644 --- a/src/Lexer.js +++ b/src/Lexer.js @@ -19,6 +19,7 @@ import {LexerInterface, Token} from "./Token"; /* The following tokenRegex * - matches typical whitespace (but not NBSP etc.) using its first group + * - matches comments (must have trailing newlines) * - does not match any control character \x00-\x1f except whitespace * - does not match a bare backslash * - matches any ASCII character except those just mentioned @@ -32,9 +33,11 @@ import {LexerInterface, Token} from "./Token"; * If there is no matching function or symbol definition, the Parser will * still reject the input. */ +const commentRegexString = "%[^\n]*[\n]"; const tokenRegex = new RegExp( "([ \r\n\t]+)|" + // whitespace - "([!-\\[\\]-\u2027\u202A-\uD7FF\uF900-\uFFFF]" + // single codepoint + `(${commentRegexString}|` + // comments + "[!-\\[\\]-\u2027\u202A-\uD7FF\uF900-\uFFFF]" + // single codepoint "|[\uD800-\uDBFF][\uDC00-\uDFFF]" + // surrogate pair "|\\\\verb\\*([^]).*?\\3" + // \verb* "|\\\\verb([^*a-zA-Z]).*?\\4" + // \verb unstarred @@ -42,6 +45,8 @@ const tokenRegex = new RegExp( ")" ); +const commentRegex = new RegExp(commentRegexString); + /** Main Lexer class */ export default class Lexer implements LexerInterface { input: string; @@ -71,6 +76,11 @@ export default class Lexer implements LexerInterface { const start = this.pos; this.pos += match[0].length; const end = this.pos; - return new Token(text, new SourceLocation(this, start, end)); + + if (commentRegex.test(text)) { + return this.lex(); + } else { + return new Token(text, new SourceLocation(this, start, end)); + } } } diff --git a/static/index.html b/static/index.html index 11abd4a0..6cfb273b 100644 --- a/static/index.html +++ b/static/index.html @@ -8,9 +8,11 @@ - +
diff --git a/static/main.css b/static/main.css index a7f178a9..ab03c979 100644 --- a/static/main.css +++ b/static/main.css @@ -1,6 +1,10 @@ body { margin: 0px; padding: 0px; + font-size: 24px; +} + +#math { font-size: 72px; } diff --git a/test/katex-spec.js b/test/katex-spec.js index 1ae324fd..6c42f5f0 100644 --- a/test/katex-spec.js +++ b/test/katex-spec.js @@ -1482,6 +1482,35 @@ describe("A font parser", function() { }); }); +describe("A comment parser", function() { + it("should parse comments at the end of a line", () => { + expect("a^2 + b^2 = c^2 % Pythagoras' Theorem\n").toParse(); + }); + + it("should parse comments at the start of a line", () => { + expect("% comment\n").toParse(); + }); + + it("should parse multiple lines of comments in a row", () => { + expect("% comment 1\n% comment 2\n").toParse(); + }); + + it("should not parse a comment that isn't followed by a newline", () => { + expect("x%y").toNotParse(); + }); + + it("should not produce or consume space", () => { + expect("\text{hello% comment 1\nworld}") + .toParseLike("\text{helloworld}"); + expect("\text{hello% comment\n\nworld}") + .toParseLike("\text{hello world}"); + }); + + it("should not include comments in the output", () => { + expect("5 % comment\n").toParseLike("5"); + }); +}); + describe("An HTML font tree-builder", function() { it("should render \\mathbb{R} with the correct font", function() { const markup = katex.renderToString("\\mathbb{R}");