|
@@ -1,14 +1,14 @@
|
|
|
-
|
|
|
|
|
import { encodeSpaces } from '@growi/core/dist/utils/page-path-utils';
|
|
import { encodeSpaces } from '@growi/core/dist/utils/page-path-utils';
|
|
|
|
|
|
|
|
export default class Linker {
|
|
export default class Linker {
|
|
|
|
|
|
|
|
- constructor(
|
|
|
|
|
- type = Linker.types.markdownLink,
|
|
|
|
|
- label = '',
|
|
|
|
|
- link = '',
|
|
|
|
|
- ) {
|
|
|
|
|
|
|
+ type: string;
|
|
|
|
|
+
|
|
|
|
|
+ label: string | undefined;
|
|
|
|
|
+
|
|
|
|
|
+ link: string | undefined;
|
|
|
|
|
|
|
|
|
|
+ constructor(type = Linker.types.markdownLink, label = '', link = '') {
|
|
|
this.type = type;
|
|
this.type = type;
|
|
|
this.label = label;
|
|
this.label = label;
|
|
|
this.link = link;
|
|
this.link = link;
|
|
@@ -33,7 +33,7 @@ export default class Linker {
|
|
|
markdownLink: /^\[(?<label>.*)\]\((?<link>.*)\)$/, // https://regex101.com/r/DZCKP3/2
|
|
markdownLink: /^\[(?<label>.*)\]\((?<link>.*)\)$/, // https://regex101.com/r/DZCKP3/2
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
- initWhenMarkdownLink() {
|
|
|
|
|
|
|
+ initWhenMarkdownLink(): void {
|
|
|
// fill label with link if empty
|
|
// fill label with link if empty
|
|
|
if (this.label === '') {
|
|
if (this.label === '') {
|
|
|
this.label = this.link;
|
|
this.label = this.link;
|
|
@@ -42,7 +42,7 @@ export default class Linker {
|
|
|
this.link = encodeSpaces(this.link);
|
|
this.link = encodeSpaces(this.link);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- generateMarkdownText() {
|
|
|
|
|
|
|
+ generateMarkdownText(): string | undefined {
|
|
|
if (this.type === Linker.types.pukiwikiLink) {
|
|
if (this.type === Linker.types.pukiwikiLink) {
|
|
|
if (this.label === '') return `[[${this.link}]]`;
|
|
if (this.label === '') return `[[${this.link}]]`;
|
|
|
return `[[${this.label}>${this.link}]]`;
|
|
return `[[${this.label}>${this.link}]]`;
|
|
@@ -56,7 +56,7 @@ export default class Linker {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// create an instance of Linker from string
|
|
// create an instance of Linker from string
|
|
|
- static fromMarkdownString(str) {
|
|
|
|
|
|
|
+ static fromMarkdownString(str: string): Linker {
|
|
|
// if str doesn't mean a linker, create a link whose label is str
|
|
// if str doesn't mean a linker, create a link whose label is str
|
|
|
let label = str;
|
|
let label = str;
|
|
|
let link = '';
|
|
let link = '';
|
|
@@ -65,23 +65,27 @@ export default class Linker {
|
|
|
// pukiwiki with separator ">".
|
|
// pukiwiki with separator ">".
|
|
|
if (str.match(this.patterns.pukiwikiLinkWithLabel)) {
|
|
if (str.match(this.patterns.pukiwikiLinkWithLabel)) {
|
|
|
type = this.types.pukiwikiLink;
|
|
type = this.types.pukiwikiLink;
|
|
|
- ({ label, link } = str.match(this.patterns.pukiwikiLinkWithLabel).groups);
|
|
|
|
|
|
|
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
|
|
|
+ ({ label, link } = str.match(this.patterns.pukiwikiLinkWithLabel)!.groups!);
|
|
|
}
|
|
}
|
|
|
// pukiwiki without separator ">".
|
|
// pukiwiki without separator ">".
|
|
|
else if (str.match(this.patterns.pukiwikiLinkWithoutLabel)) {
|
|
else if (str.match(this.patterns.pukiwikiLinkWithoutLabel)) {
|
|
|
type = this.types.pukiwikiLink;
|
|
type = this.types.pukiwikiLink;
|
|
|
- ({ label } = str.match(this.patterns.pukiwikiLinkWithoutLabel).groups);
|
|
|
|
|
|
|
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
|
|
|
+ ({ label } = str.match(this.patterns.pukiwikiLinkWithoutLabel)!.groups!);
|
|
|
link = label;
|
|
link = label;
|
|
|
}
|
|
}
|
|
|
// markdown
|
|
// markdown
|
|
|
else if (str.match(this.patterns.markdownLink)) {
|
|
else if (str.match(this.patterns.markdownLink)) {
|
|
|
type = this.types.markdownLink;
|
|
type = this.types.markdownLink;
|
|
|
- ({ label, link } = str.match(this.patterns.markdownLink).groups);
|
|
|
|
|
|
|
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
|
|
|
+ ({ label, link } = str.match(this.patterns.markdownLink)!.groups!);
|
|
|
}
|
|
}
|
|
|
// growi
|
|
// growi
|
|
|
else if (str.match(this.patterns.growiLink)) {
|
|
else if (str.match(this.patterns.growiLink)) {
|
|
|
type = this.types.growiLink;
|
|
type = this.types.growiLink;
|
|
|
- ({ label } = str.match(this.patterns.growiLink).groups);
|
|
|
|
|
|
|
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
|
|
|
+ ({ label } = str.match(this.patterns.growiLink)!.groups!);
|
|
|
link = label;
|
|
link = label;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -93,7 +97,7 @@ export default class Linker {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// create an instance of Linker from text with index
|
|
// create an instance of Linker from text with index
|
|
|
- static fromLineWithIndex(line, index) {
|
|
|
|
|
|
|
+ static fromLineWithIndex(line: string, index: number): Linker {
|
|
|
const { beginningOfLink, endOfLink } = this.getBeginningAndEndIndexOfLink(line, index);
|
|
const { beginningOfLink, endOfLink } = this.getBeginningAndEndIndexOfLink(line, index);
|
|
|
// if index is in a link, extract it from line
|
|
// if index is in a link, extract it from line
|
|
|
let linkStr = '';
|
|
let linkStr = '';
|
|
@@ -103,11 +107,11 @@ export default class Linker {
|
|
|
return this.fromMarkdownString(linkStr);
|
|
return this.fromMarkdownString(linkStr);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // return beginning and end indexies of link
|
|
|
|
|
|
|
+ // return beginning and end indices of link
|
|
|
// if index is not in a link, return { beginningOfLink: -1, endOfLink: -1 }
|
|
// if index is not in a link, return { beginningOfLink: -1, endOfLink: -1 }
|
|
|
- static getBeginningAndEndIndexOfLink(line, index) {
|
|
|
|
|
- let beginningOfLink;
|
|
|
|
|
- let endOfLink;
|
|
|
|
|
|
|
+ static getBeginningAndEndIndexOfLink(line: string, index: number): { beginningOfLink: number; endOfLink: number } {
|
|
|
|
|
+ let beginningOfLink: number;
|
|
|
|
|
+ let endOfLink: number;
|
|
|
|
|
|
|
|
// pukiwiki link ('[[link]]')
|
|
// pukiwiki link ('[[link]]')
|
|
|
[beginningOfLink, endOfLink] = this.getBeginningAndEndIndexWithPrefixAndSuffix(line, index, '[[', ']]');
|
|
[beginningOfLink, endOfLink] = this.getBeginningAndEndIndexWithPrefixAndSuffix(line, index, '[[', ']]');
|
|
@@ -130,13 +134,13 @@ export default class Linker {
|
|
|
return { beginningOfLink, endOfLink };
|
|
return { beginningOfLink, endOfLink };
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // return begin and end indexies as array only when index is between prefix and suffix and link contains containText.
|
|
|
|
|
- static getBeginningAndEndIndexWithPrefixAndSuffix(line, index, prefix, suffix, containText = '') {
|
|
|
|
|
|
|
+ // return begin and end indices as an array only when index is between prefix and suffix and link contains containText.
|
|
|
|
|
+ static getBeginningAndEndIndexWithPrefixAndSuffix(line: string, index: number, prefix: string, suffix: string, containText = ''): [number, number] {
|
|
|
const beginningIndex = line.lastIndexOf(prefix, index);
|
|
const beginningIndex = line.lastIndexOf(prefix, index);
|
|
|
- const IndexOfContainText = line.indexOf(containText, beginningIndex + prefix.length);
|
|
|
|
|
- const endIndex = line.indexOf(suffix, IndexOfContainText + containText.length);
|
|
|
|
|
|
|
+ const indexOfContainText = line.indexOf(containText, beginningIndex + prefix.length);
|
|
|
|
|
+ const endIndex = line.indexOf(suffix, indexOfContainText + containText.length);
|
|
|
|
|
|
|
|
- if (beginningIndex < 0 || IndexOfContainText < 0 || endIndex < 0) {
|
|
|
|
|
|
|
+ if (beginningIndex < 0 || indexOfContainText < 0 || endIndex < 0) {
|
|
|
return [-1, -1];
|
|
return [-1, -1];
|
|
|
}
|
|
}
|
|
|
return [beginningIndex, endIndex + suffix.length];
|
|
return [beginningIndex, endIndex + suffix.length];
|