Skip to content

Commit

Permalink
fix: set correct end line
Browse files Browse the repository at this point in the history
  • Loading branch information
agoose77 committed Apr 16, 2024
1 parent 71df326 commit 0fab1ff
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 7 deletions.
15 changes: 9 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,23 +115,26 @@ function amsmathBlock(
let { endpos } = outcome
endpos += begin

let line = startLine
while (line < endLine) {
if (endpos >= state.bMarks[line] && endpos <= state.eMarks[line]) {
// Find the final line of the amsmath block
let nextLine = startLine
for (; nextLine < endLine; nextLine++) {
if (endpos >= state.bMarks[nextLine] && endpos <= state.eMarks[nextLine]) {
// line for end of block math found ...
// eslint-disable-next-line no-param-reassign
state.line = line + 1
break
}
line += 1
}
// Skip over the final amsmath line
nextLine += 1;

Check failure on line 128 in src/index.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `;`
// Write back to the state
state.line = nextLine;

Check failure on line 130 in src/index.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `;`

if (!silent) {
const token = state.push("amsmath", "math", 0)
token.block = true
token.content = state.src.slice(begin, endpos)
token.meta = { environment, numbered }
token.map = [startLine, line]
token.map = [startLine, nextLine]
}

return true
Expand Down
55 changes: 54 additions & 1 deletion tests/fixtures.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect, it } from "vitest"
import { describe, expect, test, it } from "vitest"
import fs from "node:fs"
import katex from "katex"
import MarkdownIt from "markdown-it"
Expand Down Expand Up @@ -37,3 +37,56 @@ describe("Parses basic", () => {
it(name, () => expect(rendered.trim()).toEqual(expected.trim()))
})
})

test("Produces correct tokens", () => {
const mdit = MarkdownIt().use(amsmathPlugin)
const tokens = mdit.parse(`
\\begin{equation}
y = m * x + c \\
z = 2
\\end{equation}
Hello world!
`)
expect(tokens).toMatchObject([
{
type: "amsmath",
map: [1, 5],
nesting: 0,
level: 0,
content: "\\begin{equation}\ny = m * x + c \\\nz = 2\n\\end{equation}",
meta: { environment: "equation", numbered: "" },
block: true,
hidden: false
},
{
type: "paragraph_open",
map: [5, 6],
nesting: 1,
level: 0,
content: "",
meta: null,
block: true,
hidden: false
},
{
type: "inline",
map: [5, 6],
nesting: 0,
level: 1,
content: "Hello world!",
meta: null,
block: true,
hidden: false
},
{
type: "paragraph_close",
map: null,
nesting: -1,
level: 0,
content: "",
meta: null,
block: true,
hidden: false
}
])
})

0 comments on commit 0fab1ff

Please sign in to comment.