Commit 90cab72d authored by Alejandro Celaya's avatar Alejandro Celaya Committed by Alejandro Celaya

Add page number to every annotation when exported as text

parent 311a02d2
...@@ -17,11 +17,13 @@ describe('trimAndDedent', () => { ...@@ -17,11 +17,13 @@ describe('trimAndDedent', () => {
Hello, Jane! Hello, Jane!
Indented line Indented line
Goodbye, John! Goodbye, John!
`, `,
`Hello, Jane! `Hello, Jane!
Indented line Indented line
Goodbye, John!`, Goodbye, John!`,
], ],
[ [
...@@ -50,8 +52,7 @@ Goodbye, John! ...@@ -50,8 +52,7 @@ Goodbye, John!
multiple multiple
lines lines
with no indentation with no indentation`,
`,
], ],
[ [
() => { () => {
......
function trimLeadingEmptyLines(str: string): string { function trimLeadingAndTrailingEmptyLines(str: string): string {
return str.replace(/^\s*\n/g, ''); return str.replace(/^\s*\n|\n\s*$/g, '');
}
function trimTrailingEmptyLines(str: string): string {
return str.replace(/\n\s*$/g, '');
} }
/** /**
...@@ -71,24 +67,5 @@ export function trimAndDedent( ...@@ -71,24 +67,5 @@ export function trimAndDedent(
strings: TemplateStringsArray, strings: TemplateStringsArray,
...params: any[] ...params: any[]
): string { ): string {
if (strings.length < 2) { return trimLeadingAndTrailingEmptyLines(dedent([...strings], ...params));
// Trim leading and trailing empty lines from first (and only) string
const trimmedLines = [
trimLeadingEmptyLines(trimTrailingEmptyLines(strings[0])),
];
return dedent(trimmedLines, ...params);
}
const firstString = strings[0];
const lastString = strings[strings.length - 1];
const middle = strings.slice(1, strings.length - 1);
// Trim empty leading lines from first string, and empty trailing lines from last one
const trimmedLines = [
trimLeadingEmptyLines(firstString),
...middle,
trimTrailingEmptyLines(lastString),
];
return dedent(trimmedLines, ...params);
} }
...@@ -3,6 +3,7 @@ import type { APIAnnotationData } from '../../types/api'; ...@@ -3,6 +3,7 @@ import type { APIAnnotationData } from '../../types/api';
import { import {
documentMetadata, documentMetadata,
isReply, isReply,
pageLabel,
quote, quote,
} from '../helpers/annotation-metadata'; } from '../helpers/annotation-metadata';
import { annotationDisplayName } from '../helpers/annotation-user'; import { annotationDisplayName } from '../helpers/annotation-user';
...@@ -80,16 +81,17 @@ export class AnnotationsExporter { ...@@ -80,16 +81,17 @@ export class AnnotationsExporter {
]; ];
const annotationsText = annotations const annotationsText = annotations
.map( .map((annotation, index) => {
(annotation, index) => const page = pageLabel(annotation);
trimAndDedent` return trimAndDedent`
Annotation ${index + 1}: Annotation ${index + 1}:
${annotation.created} ${annotation.created}
${annotation.text} ${annotation.text}
${extractUsername(annotation)} ${extractUsername(annotation)}
"${quote(annotation)}" "${quote(annotation)}"
Tags: ${annotation.tags.join(', ')}`, Tags: ${annotation.tags.join(', ')}
) ${page ? `Page: ${page}` : ''}`;
})
.join('\n\n'); .join('\n\n');
return trimAndDedent` return trimAndDedent`
......
...@@ -58,7 +58,7 @@ describe('AnnotationsExporter', () => { ...@@ -58,7 +58,7 @@ describe('AnnotationsExporter', () => {
it('throws error when empty list of annotations is provided', () => { it('throws error when empty list of annotations is provided', () => {
assert.throws( assert.throws(
() => exporter.buildTextExportContent([], {}), () => exporter.buildTextExportContent([]),
'No annotations to export', 'No annotations to export',
); );
}); });
...@@ -76,6 +76,16 @@ describe('AnnotationsExporter', () => { ...@@ -76,6 +76,16 @@ describe('AnnotationsExporter', () => {
{ {
...baseAnnotation, ...baseAnnotation,
...newReply(), ...newReply(),
target: [
{
selector: [
{
type: 'PageSelector',
label: '23',
},
],
},
],
}, },
]; ];
const groupName = 'My group'; const groupName = 'My group';
...@@ -122,7 +132,8 @@ ${isoDate} ...@@ -122,7 +132,8 @@ ${isoDate}
Annotation text Annotation text
bill bill
"null" "null"
Tags: tag_1, tag_2`, Tags: tag_1, tag_2
Page: 23`,
); );
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment