Skip to content

Commit

Permalink
Merge pull request #84 from rasendubi/fix-83
Browse files Browse the repository at this point in the history
fix(#83): prevent crash when unexpected parameter is provided to export-block
  • Loading branch information
rasendubi authored Aug 24, 2023
2 parents 11ef103 + 6c1d090 commit 4021490
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
9 changes: 9 additions & 0 deletions .changeset/green-bees-matter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
'uniorg-parse': patch
---

Prevent crash when export-block has unexpected parameters.

When export-block was provided unexpected parameters, uniorg has thrown an exception and stopped parsing. org-element fails to parse export-block backend but otherwise continues to parse. We replicate the same behavior in Uniorg now.

Fixes [#83](https://github.com/rasendubi/uniorg/issues/83).
11 changes: 11 additions & 0 deletions packages/uniorg-parse/src/__snapshots__/parser.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,17 @@ children:
value: "hi\\n"
`;
exports[`org/parser blocks export block with incorrect header 1`] = `
type: "org-data"
contentsBegin: 0
contentsEnd: 55
children:
- type: "export-block"
affiliated: {}
backend: null
value: "Hi, there!\\n"
`;
exports[`org/parser blocks fake block 1`] = `
type: "org-data"
contentsBegin: 0
Expand Down
13 changes: 13 additions & 0 deletions packages/uniorg-parse/src/parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,19 @@ hi
#+end_export`
);

// Adding any parameter to export-block fails backend parsing in
// org-element, so the block is not exported. We're preserving
// this behavior in Uniorg.
//
// See https://github.com/rasendubi/uniorg/issues/83 for more
// information.
itParses(
'export block with incorrect header',
`#+begin_export html :hello blah
Hi, there!
#+end_export`
);

itParses(
'special block',
`#+begin_blah
Expand Down
4 changes: 2 additions & 2 deletions packages/uniorg-parse/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -927,10 +927,10 @@ class Parser {
return this.parseParagraph(affiliated);
}

const headerM = this.r.forceMatch(
const headerM = this.r.match(
/^[ \t]*#\+begin_export(?:[ \t]+(\S+))?[ \t]*$/im
);
const backend = headerM[1] ?? null;
const backend = headerM?.[1] ?? null;

const begin = this.r.offset();
const contentsBegin = begin + this.r.line().length;
Expand Down

0 comments on commit 4021490

Please sign in to comment.