node
URLNode
code:URLNode.js
class URLNode {
constructor({ raw, type, pathType, href, content }) {
this.raw = raw;
this.type = type;
this.pathType = pathType;
this.href = href;
this.content = content;
}
get url() {
if (this.type === "link" && this.pathType === "root") {
return https://scrapbox.io${this.href};
}
if (this.type === "hashTag" || (this.type === "link" && this.pathType === "relative")) {
return https://scrapbox.io/${scrapbox.Project.name}/${this.href};
}
if (this.type === "link" && this.pathType === "absolute") {
return this.href;
}
return;
}
openUrl() {
window.open(this.url);
}
}
export const findNodes = (condition) => {
const find = (nodes, urlNodes) => {
for (const node of nodes) {
if (condition(node)) {
urlNodes.push(
new URLNode({
raw: node.raw,
type: node.type,
pathType: node.pathType,
href: node.href,
content: node.content,
})
);
}
// 子nodesを探索
if (node.hasOwnProperty("nodes")) {
find(node.nodes, urlNodes);
}
}
};
return find;
};
export const findURLNodes = findNodes((node) => node.type === "link" || node.type === "hashTag");