Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Numbered Lists not incremental #58

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions parser/__tests__/parser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,9 @@ describe("parseParagraph", () => {
},
},
},
orderedList: {
"list-id": 1,
},
};

const paragraph = {
Expand Down Expand Up @@ -338,6 +341,7 @@ describe("parseParagraph", () => {
paragraphStyle: { namedStyleType: "HEADING_1" },
bullet: { nestingLevel: 1, listId: "list-id" },
};
documentContext.orderedList["list-id"] = 1;
const result = parseParagraph(documentContext)(heading);
expect(result).toEqual(" - # Hello, world!");
});
Expand All @@ -356,6 +360,7 @@ describe("parseParagraph", () => {
...paragraph,
bullet: { nestingLevel: 1, listId: "list-id" },
};
documentContext.orderedList["list-id"] = 1;
documentContext.lists["list-id"].listProperties.nestingLevels[1].glyphType =
"DECIMAL";
const result = parseParagraph(documentContext)(listItem);
Expand Down
17 changes: 10 additions & 7 deletions parser/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,14 @@ export const parseDoc = async (doc, answer) => {
inlineObjects: doc.inlineObjects,
lists: doc.lists || {},
suggestions: new Map(), // Accumulators for the count and total text length of all suggestions
orderedList: {},
};
let i = Object.keys(documentContext.lists).length;

while (i--) {
documentContext.orderedList[Object.keys(documentContext.lists)[i]] = 1;
}

const { paragraphs, relatedAnswerDocIDs, alternativePhrasings, glossary } =
extractDocParts(doc);

Expand All @@ -105,9 +112,7 @@ export const parseDoc = async (doc, answer) => {
}

const body = paragraphs.map(parseParagraph(documentContext)).join("\n\n");

const footnotes = extractFootnotes(documentContext, doc);

const md = body + "\n\n" + footnotes;

// Take the maximum of each suggestion's insertions and deletions
Expand Down Expand Up @@ -241,6 +246,7 @@ export const parseParagraph = (documentContext) => (paragraph) => {
const listID = pb.listId;
const list = documentContext.lists[listID];
const currentLevel = list.listProperties.nestingLevels[nestingLevel];
const orderedList = documentContext.orderedList[listID];

// This check is ugly as sin, but necessary because GDocs doesn't actually clearly say "this is an [un]ordered list" anywhere
// I think this is because internally, all lists are ordered and it just only sometimes uses glyphs which represent that
Expand All @@ -249,12 +255,9 @@ export const parseParagraph = (documentContext) => (paragraph) => {
currentLevel.hasOwnProperty("glyphType") &&
currentLevel.glyphType !== "GLYPH_TYPE_UNSPECIFIED";

// Please forgive me for always using 1. as the sequence number on list items
// It's sorta hard to count them properly so I'm depending on markdown renderers doing the heavy lifting for me.
// Which, in fairness, they're supposed to.
itemMarker = isOrdered ? "1. " : "- ";
itemMarker = isOrdered ? orderedList + ". " || "1. " : "- ";
leadingSpace = new Array(nestingLevel).fill(" ").join("");

documentContext.orderedList[listID]++;
return (
leadingSpace +
itemMarker +
Expand Down