Skip to content

Commit e7fe9f9

Browse files
committed
Allow filename and display name to be used separately or together
1 parent a6b552f commit e7fe9f9

4 files changed

Lines changed: 439 additions & 9 deletions

File tree

index.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ const css = `
1616
}
1717
`;
1818

19-
class CodeplusInstance {
19+
export class CodeplusInstance {
2020
constructor(node, options) {
21-
if (node.parentNode.tagName === "PRE") {
21+
if (node.parentNode && node.parentNode.tagName === "PRE") {
2222
// Automatically detect if we're in a pre tag
2323
this.containerNode = node.parentNode;
2424
} else {
@@ -35,18 +35,23 @@ class CodeplusInstance {
3535
this.copyButtonClass = options.copyButtonClass;
3636
this.renderCopyButton = options.renderCopyButton;
3737

38-
const parsed = parse(this.codeNode.innerText);
38+
const parsed = parse(this.codeNode.innerText || "");
3939

4040
this.filename = parsed.filename;
4141
this.displayName = parsed.displayName;
4242
this.groupName = parsed.groupName;
4343
}
4444
getNavName() {
45+
if (this.filename && this.displayName) {
46+
return `${this.filename} (${this.displayName})`;
47+
}
48+
if (this.filename) {
49+
return this.filename;
50+
}
4551
if (this.displayName) {
4652
return this.displayName;
47-
} else if (this.filename) {
48-
return this.filename;
4953
}
54+
return "";
5055
}
5156
hasNav() {
5257
return this.filename || this.displayName || this.groupName;

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"@babel/preset-env": "^7.17.10",
2121
"babel-jest": "^28.0.3",
2222
"jest": "^28.0.3",
23+
"jest-environment-jsdom": "^28.0.2",
2324
"parcel": "^2.5.0",
2425
"prettier": "^2.6.2"
2526
},

test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1+
/**
2+
* @jest-environment jsdom
3+
*/
4+
15
import parse from "./parse";
6+
import {CodeplusInstance} from "./index"
27

38
test("parse filename", () => {
49
const parsed = parse("# filename.js\nfunction foo() {}");
@@ -44,3 +49,25 @@ test("parse filename, display, group", () => {
4449
groupName: "group example",
4550
});
4651
});
52+
53+
test("nav with filename", () => {
54+
const node = document.createElement("code");
55+
const instance = new CodeplusInstance(node, {});
56+
instance.filename = "filename.js";
57+
expect(instance.getNavName()).toEqual("filename.js");
58+
});
59+
60+
test("nav with display name", () => {
61+
const node = document.createElement("code");
62+
const instance = new CodeplusInstance(node, {});
63+
instance.displayName = "Test";
64+
expect(instance.getNavName()).toEqual("Test");
65+
});
66+
67+
test("nav with filename and display", () => {
68+
const node = document.createElement("code");
69+
const instance = new CodeplusInstance(node, {});
70+
instance.filename = "filename.js";
71+
instance.displayName = "Test";
72+
expect(instance.getNavName()).toEqual("filename.js (Test)");
73+
});

0 commit comments

Comments
 (0)