From 4c20bcb0534c7410bd00da50da5e6ffdaf9f4f02 Mon Sep 17 00:00:00 2001
From: marihachi <marihachi0620@gmail.com>
Date: Thu, 22 Jul 2021 20:30:46 +0900
Subject: [PATCH] add brackets property of url node

---
 docs/syntax.md            | 24 ++++++++++++++++++++++++
 src/internal/parser.pegjs |  2 +-
 src/internal/util.ts      |  7 ++++++-
 src/node.ts               |  3 ++-
 test/parser.ts            |  2 +-
 5 files changed, 34 insertions(+), 4 deletions(-)

diff --git a/docs/syntax.md b/docs/syntax.md
index 825e655..fe73ead 100644
--- a/docs/syntax.md
+++ b/docs/syntax.md
@@ -475,6 +475,7 @@ http://hoge.jp/abc
 ```
 
 ## ノード
+構文1:
 ```js
 {
   type: 'url',
@@ -484,6 +485,29 @@ http://hoge.jp/abc
 }
 ```
 
+または
+
+```js
+{
+  type: 'url',
+  props: {
+    url: 'https://misskey.io/@ai',
+    brackets: false
+  }
+}
+```
+
+構文2:
+```js
+{
+  type: 'url',
+  props: {
+    url: 'https://misskey.io/@ai',
+    brackets: true
+  }
+}
+```
+
 ## 詳細
 - インライン構文。
 
diff --git a/src/internal/parser.pegjs b/src/internal/parser.pegjs
index 630957e..fda5a19 100644
--- a/src/internal/parser.pegjs
+++ b/src/internal/parser.pegjs
@@ -362,7 +362,7 @@ hashtagChar
 url
 	= "<" url:altUrlFormat ">"
 {
-	return N_URL(url);
+	return N_URL(url, true);
 }
 	/ url:urlFormat
 {
diff --git a/src/internal/util.ts b/src/internal/util.ts
index 9bfec38..39ab597 100644
--- a/src/internal/util.ts
+++ b/src/internal/util.ts
@@ -79,7 +79,12 @@ export function stringifyNode(node: MfmNode): string {
 			return `#${ node.props.hashtag }`;
 		}
 		case 'url': {
-			return node.props.url;
+			if (node.props.brackets) {
+				return `<${ node.props.url }>`;
+			}
+			else {
+				return node.props.url;
+			}
 		}
 		case 'link': {
 			const prefix = node.props.silent ? '?' : '';
diff --git a/src/node.ts b/src/node.ts
index ac1e29f..03c8453 100644
--- a/src/node.ts
+++ b/src/node.ts
@@ -143,10 +143,11 @@ export type MfmUrl = {
 	type: 'url';
 	props: {
 		url: string;
+		brackets?: boolean;
 	};
 	children?: [];
 };
-export const N_URL = (value: string): NodeType<'url'> => { return { type:'url', props: { url: value } }; };
+export const N_URL = (value: string, brackets?: boolean): NodeType<'url'> => { return { type:'url', props: { url: value, brackets } }; };
 
 export type MfmLink = {
 	type: 'link';
diff --git a/test/parser.ts b/test/parser.ts
index eda18da..489c821 100644
--- a/test/parser.ts
+++ b/test/parser.ts
@@ -878,7 +878,7 @@ hoge`;
 		it('match non-ascii characters contained url with angle brackets', () => {
 			const input = '<https://大石泉すき.example.com>';
 			const output = [
-				N_URL('https://大石泉すき.example.com'),
+				N_URL('https://大石泉すき.example.com', true),
 			];
 			assert.deepStrictEqual(mfm.parse(input), output);
 		});
-- 
GitLab