|
|
@@ -1,10 +1,25 @@
|
|
|
+import path from 'path';
|
|
|
+
|
|
|
export default class Linker {
|
|
|
|
|
|
- constructor(type, label, link) {
|
|
|
+ constructor(
|
|
|
+ type,
|
|
|
+ label,
|
|
|
+ link,
|
|
|
+ isUseRelativePath = false,
|
|
|
+ rootPath = '',
|
|
|
+ isUsePermanentLink = false,
|
|
|
+ permalink = '',
|
|
|
+ ) {
|
|
|
this.type = type;
|
|
|
this.label = label;
|
|
|
this.link = link;
|
|
|
- // TODO GW-3074 相対パスを利用しているかの情報も持つようにする
|
|
|
+ this.isUseRelativePath = isUseRelativePath;
|
|
|
+ this.rootPath = rootPath;
|
|
|
+ this.isUsePermanentLink = isUsePermanentLink;
|
|
|
+ this.permalink = permalink;
|
|
|
+
|
|
|
+ this.generateMarkdownText = this.generateMarkdownText.bind(this);
|
|
|
}
|
|
|
|
|
|
static types = {
|
|
|
@@ -20,6 +35,28 @@ export default class Linker {
|
|
|
markdownLink: /^\[(?<label>.*)\]\((?<link>.*)\)$/, // https://regex101.com/r/DZCKP3/2
|
|
|
}
|
|
|
|
|
|
+ generateMarkdownText() {
|
|
|
+ let reshapedLink = this.link;
|
|
|
+
|
|
|
+ if (this.isUseRelativePath && this.link.match(/^\//)) {
|
|
|
+ reshapedLink = path.relative(this.rootPath, this.link);
|
|
|
+ }
|
|
|
+ if (this.isUsePermanentLink && this.permalink != null) {
|
|
|
+ reshapedLink = this.permalink;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (this.type === Linker.types.pukiwikiLink) {
|
|
|
+ if (this.label === reshapedLink) return `[[${reshapedLink}]]`;
|
|
|
+ return `[[${this.label}>${reshapedLink}]]`;
|
|
|
+ }
|
|
|
+ if (this.type === Linker.types.growiLink) {
|
|
|
+ return `[${reshapedLink}]`;
|
|
|
+ }
|
|
|
+ if (this.type === Linker.types.markdownLink) {
|
|
|
+ return `[${this.label}](${reshapedLink})`;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
// create an instance of Linker from string
|
|
|
static fromMarkdownString(str) {
|
|
|
// if str doesn't mean a linker, create a link whose label is str
|
|
|
@@ -50,7 +87,19 @@ export default class Linker {
|
|
|
({ label, link } = str.match(this.patterns.markdownLink).groups);
|
|
|
}
|
|
|
|
|
|
- return new Linker(type, label, link);
|
|
|
+ // TODO GW-3074 相対パスを利用しているかテキストから判定し以下の値に反映する
|
|
|
+ const isUseRelativePath = false;
|
|
|
+ const rootPath = '';
|
|
|
+
|
|
|
+ return new Linker(
|
|
|
+ type,
|
|
|
+ label,
|
|
|
+ link,
|
|
|
+ isUseRelativePath,
|
|
|
+ rootPath,
|
|
|
+ false,
|
|
|
+ '',
|
|
|
+ );
|
|
|
}
|
|
|
|
|
|
// create an instance of Linker from text with index
|