Skip to content

Handle BOM and other issues found in forum issue #25

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

Open
wants to merge 4 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
7 changes: 5 additions & 2 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const createNoteValue = (k, v, sep = ' --- ') => {
}

const createValue = (k, v) => {
if (!v) return
if (!v) return {}
switch (k) {
case (`${TROPY}#note`):
return createNoteValue(k, v)
Expand Down Expand Up @@ -90,7 +90,10 @@ function getProtocolAndPath(path, relativeTo) {
}
}
const parseProtocol = (photo, baseDirectory) => {
const rawPath = photo[`${TROPY}#path`][0]['@value']
const rawPath = photo[`${TROPY}#path`]?.[0]['@value']
if (!rawPath) {
return photo
}
const { protocol, path } = getProtocolAndPath(rawPath, baseDirectory)
photo[`${TROPY}#path`][0]['@value'] = path
Object.assign(photo, createValue(`${TROPY}#protocol`, protocol))
Expand Down
21 changes: 15 additions & 6 deletions src/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,15 @@ class CSVPlugin {
let photoData = Object.assign(
...p.map((v, idx) => createValue(photoKeys[idx], v))
)
parseProtocol(photoData, baseDirectory)
item.photo.push(addTemplateKey(photoData, this.options.photoTemplate))
if (photoData[`${TROPY}#path`] !== undefined) {
parseProtocol(photoData, baseDirectory)
item.photo.push(
addTemplateKey(photoData, this.options.photoTemplate)
)
} else {
// Don't fail if there's no TROPY/#path values for that row
console.warn('No photo paths parsed for row', row)
}
}
}
return item
Expand All @@ -225,10 +232,12 @@ class CSVPlugin {

for (const file of files) {
try {
const csvRows = parse(
await readFile(file),
{ relaxColumnCount: true, delimiter: this.options.delimiter }
)
const csvRows = parse(await readFile(file), {
relaxColumnCount: true,
delimiter: this.options.delimiter,
bom: true,
skipEmptyLines: true
})
const headerRow = this.options.customHeaders ?
parse(this.options.customHeaders,
{ delimiter: this.options.delimiter }
Expand Down
27 changes: 23 additions & 4 deletions test/import_row_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const photoHeaders = [
`${TROPY}#note`
]
const rowNoPhoto = ['2022-01-02', 'title', '']
const rowNoTitle = ['2022-01-02', '', '']
const rowNoDate = ['', 'title', '']
const rowSinglePhoto = [
'2022-01-03',
'title1',
Expand All @@ -31,6 +31,17 @@ const rowManyPhoto = [
(platform === 'win32') ? 'D:\\user\\photo2.jpg' : '/home/user/photo2.jpg',
'some note --- another note, with a comma']

const rowMissingPhoto = [
'2022-01-04',
'title2',
'tag2',
(platform === 'win32') ? 'D:\\user\\photo1.jpg' : '/home/user/photo1.jpg',
'a note',
(platform === 'win32') ? 'D:\\user\\photo2.jpg' : '/home/user/photo2.jpg',
'some note --- another note, with a comma',
// No photo path provided
'', 'this note should be dropped?']

function generatePhoto(path, note = null, protocol = 'file') {
const photo = {
'https://tropy.org/v1/tropy#path': [{ '@value': path }],
Expand Down Expand Up @@ -62,9 +73,9 @@ describe('Parse row', () => {
assert.deepEqual(actual, generateExpectedItem(rowNoPhoto[0], rowNoPhoto[1]))
})
it('Does not add key to item if value is empty in csv', () => {
const actual = plugin.parseRow(rowNoTitle, itemHeaders, photoHeaders)
assert.equal(actual['http://purl.org/dc/terms/title'], undefined)
assert.ok(actual['http://purl.org/dc/elements/1.1/date'] !== undefined)
const actual = plugin.parseRow(rowNoDate, itemHeaders, photoHeaders)
assert.ok(actual['http://purl.org/dc/terms/title'] !== undefined)
assert.equal(actual['http://purl.org/dc/elements/1.1/date'], undefined)
})
it('Item has no photos if no photo headers present', () => {
const actual = plugin.parseRow(rowSinglePhoto, itemHeaders, undefined)
Expand All @@ -82,6 +93,14 @@ describe('Parse row', () => {
assert.deepEqual(actual['photo'][0],
generatePhoto(rowManyPhoto[3], rowManyPhoto[4]))
})
it('Photo and note not imported if no path for photos', () => {
const actual = plugin.parseRow(rowMissingPhoto, itemHeaders, photoHeaders)
assert.equal(actual.photo.length, 2)
assert.equal(
JSON.stringify(actual).includes('This note should be dropped?'),
false
)
})
it('Photo has no notes if notes not present in CSV', () => {
const actual = plugin.parseRow(rowSinglePhoto, itemHeaders, photoHeaders)
assert.equal(actual.photo.length, 1)
Expand Down