aboutsummaryrefslogtreecommitdiff
path: root/src/client/apis/google_docs/GoogleApiClientUtils.ts
diff options
context:
space:
mode:
authorSam Wilkins <samwilkins333@gmail.com>2019-08-30 17:28:34 -0400
committerSam Wilkins <samwilkins333@gmail.com>2019-08-30 17:28:34 -0400
commite139441a1f3bbec9a51ef8594a9c785733d28415 (patch)
tree39342dce6c1699e746a4c58dce78fbbddaa520aa /src/client/apis/google_docs/GoogleApiClientUtils.ts
parentc9a7d747916c31730f71479d8e516ba0ed2c658f (diff)
restructured paragraphs
Diffstat (limited to 'src/client/apis/google_docs/GoogleApiClientUtils.ts')
-rw-r--r--src/client/apis/google_docs/GoogleApiClientUtils.ts32
1 files changed, 21 insertions, 11 deletions
diff --git a/src/client/apis/google_docs/GoogleApiClientUtils.ts b/src/client/apis/google_docs/GoogleApiClientUtils.ts
index fdd708e31..3026f6e17 100644
--- a/src/client/apis/google_docs/GoogleApiClientUtils.ts
+++ b/src/client/apis/google_docs/GoogleApiClientUtils.ts
@@ -94,29 +94,39 @@ export namespace GoogleApiClientUtils {
export namespace Utils {
- export type ExtractResult = { text: string, runs: docs_v1.Schema$TextRun[] };
+ export type ExtractResult = { text: string, paragraphs: DeconstructedParagraph[] };
export const extractText = (document: docs_v1.Schema$Document, removeNewlines = false): ExtractResult => {
- let runs = extractTextRuns(document);
- let text = runs.map(run => run.content).join("");
+ let paragraphs = extractParagraphs(document);
+ let text = paragraphs.map(paragraph => paragraph.runs.map(run => run.content).join("")).join("");
text = text.substring(0, text.length - 1);
removeNewlines && text.ReplaceAll("\n", "");
- return { text, runs };
+ return { text, paragraphs };
};
- const extractTextRuns = (document: docs_v1.Schema$Document, filterEmpty = true) => {
- const fragments: docs_v1.Schema$TextRun[] = [];
+ export type DeconstructedParagraph = { runs: docs_v1.Schema$TextRun[], bullet: Opt<number> };
+ const extractParagraphs = (document: docs_v1.Schema$Document, filterEmpty = true): DeconstructedParagraph[] => {
+ const fragments: DeconstructedParagraph[] = [];
if (document.body && document.body.content) {
for (const element of document.body.content) {
- if (element.paragraph && element.paragraph.elements) {
- for (const inner of element.paragraph.elements) {
- if (inner && inner.textRun) {
- fragments.push(inner.textRun);
+ let runs: docs_v1.Schema$TextRun[] = [];
+ let bullet: Opt<number>;
+ if (element.paragraph) {
+ if (element.paragraph.elements) {
+ for (const inner of element.paragraph.elements) {
+ if (inner && inner.textRun) {
+ let run = inner.textRun;
+ (run.content || !filterEmpty) && runs.push(inner.textRun);
+ }
}
}
+ if (element.paragraph.bullet) {
+ bullet = element.paragraph.bullet.nestingLevel || 0;
+ }
}
+ runs.length && fragments.push({ runs, bullet });
}
}
- return filterEmpty ? fragments.filter(run => run.content) : fragments;
+ return fragments;
};
export const endOf = (schema: docs_v1.Schema$Document): number | undefined => {