Skip to content

Commit

Permalink
fix note export
Browse files Browse the repository at this point in the history
  • Loading branch information
codyzu committed Oct 27, 2023
1 parent b4e21f7 commit 0df271d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
33 changes: 33 additions & 0 deletions src/components/slides/parse-notes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,39 @@ describe('parse notes', () => {
expect(output).toEqual(expectedMarkdown);
});

it('parses sequential notes', () => {
const input: Note[] = [
{pageIndices: [0], markdown: 'abc'},
{pageIndices: [1], markdown: 'def'},
];
expectedMarkdown = `<!-- +1+1 -->
abc
<!-- +1+1 -->
def
`;

const output = exportNotes(input);
expect(output).toEqual(expectedMarkdown);
});

it('parses sequential notes with empty notes in between', () => {
const input: Note[] = [
{pageIndices: [0], markdown: ''},
{pageIndices: [1], markdown: 'abc'},
{pageIndices: [2], markdown: 'def'},
];
expectedMarkdown = `<!-- +2+1 -->
abc
<!-- +1+1 -->
def
`;

const output = exportNotes(input);
expect(output).toEqual(expectedMarkdown);
});

it('imports notes', async () => {
const input = expectedMarkdown;

Expand Down
9 changes: 5 additions & 4 deletions src/components/slides/parse-notes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export function importNotes(markdown: string, pageCount: number): Note[] {
// Grab the next section, but make sure it isn't a magic comment
if (!magicCommentRegEx.test(noteSections[sectionIndex + 1])) {
markdown = noteSections[sectionIndex + 1];
// Advance the section index for the next interation
// Advance the section index for the next iteration
sectionIndex++;
}

Expand All @@ -85,17 +85,18 @@ export function importNotes(markdown: string, pageCount: number): Note[] {
}

export function exportNotes(notes: Note[]): string {
let currentOffset = 0;
let previousEndIndex = -1;
return notes
.map((note) => {
// Remove empty notes
if (note.markdown.trim().length === 0) {
return undefined;
}

const startIndex = note.pageIndices[0] - currentOffset + 1;
const startIndex = note.pageIndices[0] - previousEndIndex;
const noteLength = note.pageIndices.length;
currentOffset += noteLength;
// -1 (exported notes are 1 indexed) -1 (lengths are assumed at least 1)
previousEndIndex = startIndex + noteLength - 1 - 1;

return `<!-- +${startIndex}+${noteLength} -->\n${note.markdown}\n`;
})
Expand Down

0 comments on commit 0df271d

Please sign in to comment.