Skip to content

Commit

Permalink
Fix:Parse series sequence from OPF in cases where series_index is not…
Browse files Browse the repository at this point in the history
… directly underneath series meta #2505
  • Loading branch information
advplyr committed Jan 9, 2024
1 parent 69e23ef commit da25eff
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
13 changes: 11 additions & 2 deletions server/utils/parsers/parseOpfMetadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,24 @@ function fetchSeries(metadataMeta) {
if (!metadataMeta) return []
const result = []
for (let i = 0; i < metadataMeta.length; i++) {
if (metadataMeta[i].$?.name === "calibre:series" && metadataMeta[i].$.content?.trim()) {
if (metadataMeta[i].$?.name === 'calibre:series' && metadataMeta[i].$.content?.trim()) {
const name = metadataMeta[i].$.content.trim()
let sequence = null
if (metadataMeta[i + 1]?.$?.name === "calibre:series_index" && metadataMeta[i + 1].$?.content?.trim()) {
if (metadataMeta[i + 1]?.$?.name === 'calibre:series_index' && metadataMeta[i + 1].$?.content?.trim()) {
sequence = metadataMeta[i + 1].$.content.trim()
}
result.push({ name, sequence })
}
}

// If one series was found with no series_index then check if any series_index meta can be found
// this is to support when calibre:series_index is not directly underneath calibre:series
if (result.length === 1 && !result[0].sequence) {
const seriesIndexMeta = metadataMeta.find(m => m.$?.name === 'calibre:series_index' && m.$.content?.trim())
if (seriesIndexMeta) {
result[0].sequence = seriesIndexMeta.$.content.trim()
}
}
return result
}

Expand Down
17 changes: 17 additions & 0 deletions test/server/utils/parsers/parseOpfMetadata.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,21 @@ describe('parseOpfMetadata - test series', async () => {
{ "name": "Serie 1", "sequence": null }
])
})

it('test series and series index not directly underneath', async () => {
const opf = `
<?xml version='1.0' encoding='UTF-8'?>
<package xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:opf="http://www.idpf.org/2007/opf" xml:lang="en" version="3.0" unique-identifier="bookid">
<metadata>
<meta name="calibre:series" content="Serie 1"/>
<meta name="calibre:title_sort" content="Test Title"/>
<meta name="calibre:series_index" content="1"/>
</metadata>
</package>
`
const parsedOpf = await parseOpfMetadataXML(opf)
expect(parsedOpf.series).to.deep.equal([
{ "name": "Serie 1", "sequence": "1" }
])
})
})

0 comments on commit da25eff

Please sign in to comment.