Skip to content

Commit

Permalink
fix how hrule splits chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
laughedelic committed Nov 15, 2024
1 parent 552da7e commit cf8046e
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 4 deletions.
62 changes: 62 additions & 0 deletions outline.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,68 @@ and more of this mess
});
});

Deno.test("outlineMarkdown handling mixed lists and headings", async (t) => {
await t.step("should handle lists before and after headings", () => {
const input = `
- one
- two
# h1
- three
`.trim();

const expectedOutput = `
- one
- two
- # h1
- three
`.trim();

assertEquals(outlineMarkdown(input), expectedOutput);
});

await t.step("heading should split the list without blank lines", () => {
const input = `
- one
- two
# h1
- three
`.trim();

const expectedOutput = `
- one
- two
- # h1
- three
`.trim();

assertEquals(outlineMarkdown(input), expectedOutput);
});

await t.step("hrule should end the list", () => {
const input = `
- one
- two
---
# h1
- three
`.trim();

const expectedOutput = `
- one
- two
- ---
- # h1
- three
`.trim();

assertEquals(outlineMarkdown(input), expectedOutput);
});
});

Deno.test("outlineMarkdown with listNesting option", async (t) => {
const input = `
# h1
Expand Down
9 changes: 5 additions & 4 deletions outline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export function splitIntoChunks(markdown: string): Chunk[] {
let currentType: Chunk["type"] | null = null;
let inCodeBlock = false;
let inBlockQuote = 0;
let isUnbreakableBlock = false;

function commitChunk() {
if (currentChunk.length > 0 && currentType) {
Expand All @@ -20,7 +21,7 @@ export function splitIntoChunks(markdown: string): Chunk[] {
: 0;
chunks.push({ type: currentType, content, level });
currentChunk = [];
// console.log("committing chunk", currentType, content);
console.log("committing chunk", currentType, "\n", content, "\n");
currentType = null;
}
}
Expand All @@ -32,7 +33,7 @@ export function splitIntoChunks(markdown: string): Chunk[] {
const isBlockEnd = line.trim().startsWith("#+END_");

// If the first chunk is a frontmatter block, pass through it and commit as-is
if (!chunks.length && frontmatterDelimiter) {
if (!chunks.length && !currentType && frontmatterDelimiter) {
currentType = "frontmatter";
continue;
}
Expand All @@ -47,7 +48,7 @@ export function splitIntoChunks(markdown: string): Chunk[] {
continue;
}

if (!line.trim() && !inCodeBlock) {
if (!isUnbreakableBlock && !line.trim()) {
if (currentType !== "list") {
commitChunk();
}
Expand Down Expand Up @@ -82,7 +83,7 @@ export function splitIntoChunks(markdown: string): Chunk[] {
inBlockQuote = Math.max(inBlockQuote - 1, 0);
}

const isUnbreakableBlock = inBlockQuote > 0 || inCodeBlock;
isUnbreakableBlock = inBlockQuote > 0 || inCodeBlock;

const isListEnd = !isList && currentType === "list";

Expand Down

0 comments on commit cf8046e

Please sign in to comment.