Skip to content

Commit

Permalink
Merge pull request #73 from StampyAI/issue-64-blockquotes-murphant
Browse files Browse the repository at this point in the history
Add blockquote functionality
  • Loading branch information
LeMurphant authored Nov 15, 2024
2 parents b86afb0 + 18dd9dc commit c4f220c
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 3 deletions.
57 changes: 57 additions & 0 deletions parser/__tests__/parser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,63 @@ describe("parseParagraph", () => {
const nestingSpacer = " ";
expect(result2).toEqual(nestingSpacer + "1. Hello, world!");
});

it("should format indented text as block quotes", () => {
const indentedParagraph = {
elements: [{ textRun: { content: "This is an indented quote" } }],
paragraphStyle: {
namedStyleType: "NORMAL_TEXT",
indentStart: { magnitude: 36 }, // Standard indentation button level
},
};
const result = parseParagraph(documentContext)(indentedParagraph);
expect(result).toEqual("> This is an indented quote");
});

it("should not format text with small indentation as block quotes", () => {
const slightlyIndentedParagraph = {
elements: [{ textRun: { content: "This has small indentation" } }],
paragraphStyle: {
namedStyleType: "NORMAL_TEXT",
indentStart: { magnitude: 10 }, // Below our threshold of 18
},
};
const result = parseParagraph(documentContext)(slightlyIndentedParagraph);
expect(result).toEqual("This has small indentation");
});

it("should handle multiline block quotes", () => {
const multilineParagraph = {
elements: [
{ textRun: { content: "First line\nSecond line\nThird line" } },
],
paragraphStyle: {
namedStyleType: "NORMAL_TEXT",
indentStart: { magnitude: 36 },
},
};
const result = parseParagraph(documentContext)(multilineParagraph);
expect(result).toEqual("> First line\n> Second line\n> Third line");
});

it("should handle block quotes with other formatting", () => {
const formattedParagraph = {
elements: [
{
textRun: {
content: "Bold quote",
textStyle: { bold: true },
},
},
],
paragraphStyle: {
namedStyleType: "NORMAL_TEXT",
indentStart: { magnitude: 36 },
},
};
const result = parseParagraph(documentContext)(formattedParagraph);
expect(result).toEqual("> **Bold quote**");
});
});

describe("parseDoc", () => {
Expand Down
18 changes: 15 additions & 3 deletions parser/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,16 @@ export const mergeSameElements = (elements) =>
return acc;
}, []);

const isQuote = (paragraphStyle) => {
const indentStart = paragraphStyle?.indentStart?.magnitude || 0;
const QUOTE_INDENT_THRESHOLD = 18; // Standard indentation button is 36pt, we test lower
return indentStart >= QUOTE_INDENT_THRESHOLD;
};

export const parseParagraph = (documentContext) => (paragraph) => {
const { elements, ...paragraphContext } = paragraph;
const paragraphStyleName = paragraphContext.paragraphStyle?.namedStyleType;
const paragraphStyle = paragraphContext.paragraphStyle || {};
const paragraphStyleName = paragraphStyle.namedStyleType;

let md = mergeSameElements(elements).map(
parseElement({ documentContext, paragraphContext })
Expand All @@ -250,7 +257,7 @@ export const parseParagraph = (documentContext) => (paragraph) => {
let leadingSpace = "";

// First we check if the "paragraph" is a heading, because the markdown for a heading is the first thing we need to output
if (paragraphStyleName.indexOf("HEADING_") === 0) {
if (paragraphStyleName?.indexOf("HEADING_") === 0) {
const headingLevel = parseInt(paragraphStyleName[8]);
const headingPrefix = new Array(headingLevel).fill("#").join("") + " ";
prefix = headingPrefix;
Expand Down Expand Up @@ -296,11 +303,16 @@ export const parseParagraph = (documentContext) => (paragraph) => {
md.join("").replaceAll("\n", "\n" + leadingSpace + " ")
);
} else {
let quotePrefix = "";
if (isQuote(paragraphStyle)) {
quotePrefix = "> ";
}
return (
leadingSpace +
itemMarker +
quotePrefix +
prefix +
md.join("").replaceAll("\n", "\n" + leadingSpace)
md.join("").replaceAll("\n", "\n" + leadingSpace + quotePrefix)
);
}
};
Expand Down

0 comments on commit c4f220c

Please sign in to comment.