Skip to content

Commit

Permalink
Refactor passageWork using URN module
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobwegner committed Dec 28, 2023
1 parent 6dffe8b commit 63852b2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 15 deletions.
37 changes: 31 additions & 6 deletions packages/common/src/URN.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export default class URN {
this.nss = null;
this.textGroup = null;
this.work = null;
this.versionPart = null;
this.workPart = null;
this.reference = null;
this.version = null;
this.destructureUrn();
Expand All @@ -16,18 +18,19 @@ export default class URN {
destructureUrn() {
const split = this.absolute.split(this.delimiter);
[this.scheme, this.nid, this.nss, this.work, this.reference] = split;
this.version = `${this.scheme}:${this.nid}:${this.nss}:${this.work}:`;
// eslint-disable-next-line prefer-destructuring
this.textGroup = this.version
.split('.')[0]
.split(':')
.slice(-1)[0];
this.version = `${this.urnPrefix}:${this.work}:`;
const versionParts = this.version.split(':').slice(-2)[0].split('.');
[this.textGroup, this.workPart, this.versionPart] = versionParts;
}

toString() {
return this.absolute;
}

get urnPrefix() {
return `${this.scheme}:${this.nid}:${this.nss}`;
}

get lcp() {
return this.reference
? this.reference
Expand All @@ -36,4 +39,26 @@ export default class URN {
.slice(-1)[0]
: null;
}

upTo(segment) {
// NOTE: Backported from https://github.com/scaife-viewer/scaife-viewer/blob/59fbb4eb6bd886285eb27bf95c5ea0ce578f2b3e/static/src/js/urn.js#L32C1-L49C4
const segments = [this.urnPrefix, ':'];
if (segment === 'textGroup') {
segments.push(this.textGroup, ':');
return segments.join('');
}
if (segment === 'work') {
segments.push(this.textGroup, '.');
segments.push(this.workPart, ':');
return segments.join('');
}
if (segment === 'version') {
return this.version
}
if (segment === 'reference') {
return this.absolute;
}
return null;
}

}
10 changes: 1 addition & 9 deletions packages/widget-toc/src/TOCWidget.vue
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,7 @@
},
computed: {
passageWork() {
// FIXME: Fix this so we have "upTo" in URN module
if (this.passage) {
const [tgPart, workPart, ] = `${this.passage}`
.split(`${this.passage.nss}:`)[1]
.split(':')[0]
.split('.');
return `urn:cts:${this.passage.nss}:${tgPart}.${workPart}:`;
}
return '';
return this.passage ? this.passage.upTo('work') : '';
},
emptyMessage() {
return SHOW_RELEVANT_ONLY
Expand Down

0 comments on commit 63852b2

Please sign in to comment.