This is an automated email from the ASF dual-hosted git repository.

shuai pushed a commit to branch test
in repository https://gitbox.apache.org/repos/asf/incubator-answer.git

commit 0893e460e59d06a2cb81b2b384e597f66e2c02ba
Author: hgaol <[email protected]>
AuthorDate: Thu Jul 11 21:51:19 2024 +0800

    support prefill with markdown format
---
 ui/package.json                      |  2 ++
 ui/pnpm-lock.yaml                    | 56 ++++++++++++++++++++++++++++++++++++
 ui/src/index.tsx                     |  4 +++
 ui/src/pages/Questions/Ask/index.tsx | 42 +++++++++++++++------------
 4 files changed, 85 insertions(+), 19 deletions(-)

diff --git a/ui/package.json b/ui/package.json
index 5fc8b553..a918f027 100644
--- a/ui/package.json
+++ b/ui/package.json
@@ -23,12 +23,14 @@
     "axios": "^0.28.1",
     "bootstrap": "^5.3.2",
     "bootstrap-icons": "^1.10.5",
+    "buffer": "^6.0.3",
     "classnames": "^2.3.1",
     "codemirror": "^6.0.1",
     "color": "^4.2.3",
     "copy-to-clipboard": "^3.3.2",
     "dayjs": "^1.11.5",
     "diff": "^5.1.0",
+    "gray-matter": "^4.0.3",
     "i18next": "^21.9.0",
     "lodash": "^4.17.21",
     "marked": "^4.0.19",
diff --git a/ui/pnpm-lock.yaml b/ui/pnpm-lock.yaml
index 3124ae49..244d7ad2 100644
--- a/ui/pnpm-lock.yaml
+++ b/ui/pnpm-lock.yaml
@@ -29,6 +29,9 @@ importers:
       bootstrap-icons:
         specifier: ^1.10.5
         version: 1.10.5
+      buffer:
+        specifier: ^6.0.3
+        version: 6.0.3
       classnames:
         specifier: ^2.3.1
         version: 2.3.2
@@ -47,6 +50,9 @@ importers:
       diff:
         specifier: ^5.1.0
         version: 5.1.0
+      gray-matter:
+        specifier: ^4.0.3
+        version: 4.0.3
       i18next:
         specifier: ^21.9.0
         version: 21.9.2
@@ -4392,6 +4398,10 @@ packages:
   /[email protected]:
     resolution: {integrity: 
sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==}
 
+  /[email protected]:
+    resolution: {integrity: 
sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==}
+    dev: false
+
   /[email protected]:
     resolution: {integrity: 
sha512-x+VAiMRL6UPkx+kudNvxTl6hB2XNNCG2r+7wixVfIYwu/2HKRXimwQyaumLjMveWvT2Hkd/cAJw+QBMfJ/EKVw==}
 
@@ -4523,6 +4533,13 @@ packages:
   /[email protected]:
     resolution: {integrity: 
sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ==}
 
+  /[email protected]:
+    resolution: {integrity: 
sha512-FTiCpNxtwiZZHEZbcbTIcZjERVICn9yq/pDFkTl95/AxzD1naBctN7YO68riM/gLSDY7sdrMby8hofADYuuqOA==}
+    dependencies:
+      base64-js: 1.5.1
+      ieee754: 1.2.1
+    dev: false
+
   /[email protected]:
     resolution: {integrity: 
sha512-zhaCDicdLuWN5UbN5IMnFqNMhNfo919sH85y2/ea+5Yg9TsTkeZxpL+JLbp6cgYFS4sRLp3YV4S6yDuqVWHYOw==}
     engines: {node: '>=6'}
@@ -6419,6 +6436,13 @@ packages:
     transitivePeerDependencies:
       - supports-color
 
+  /[email protected]:
+    resolution: {integrity: 
sha512-zCnTtlxNoAiDc3gqY2aYAWFx7XWWiasuF2K8Me5WbN8otHKTUKBwjPtNpRs/rbUZm7KxWAaNj7P1a/p52GbVug==}
+    engines: {node: '>=0.10.0'}
+    dependencies:
+      is-extendable: 0.1.1
+    dev: false
+
   /[email protected]:
     resolution: {integrity: 
sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==}
 
@@ -6821,6 +6845,16 @@ packages:
   /[email protected]:
     resolution: {integrity: 
sha512-EtKwoO6kxCL9WO5xipiHTZlSzBm7WLT627TqC/uVRd0HKmq8NXyebnNYxDoBi7wt8eTWrUrKXCOVaFq9x1kgag==}
 
+  /[email protected]:
+    resolution: {integrity: 
sha512-5v6yZd4JK3eMI3FqqCouswVqwugaA9r4dNZB1wwcmrD02QkV5H0y7XBQW8QwQqEaZY1pM9aqORSORhJRdNK44Q==}
+    engines: {node: '>=6.0'}
+    dependencies:
+      js-yaml: 3.14.1
+      kind-of: 6.0.3
+      section-matter: 1.0.0
+      strip-bom-string: 1.0.0
+    dev: false
+
   /[email protected]:
     resolution: {integrity: 
sha512-ax7ZYomf6jqPTQ4+XCpUGyXKHk5WweS+e05MBO4/y3WJ5RkmPXNKvX+bx1behVILVwr6JSQvZAku021CHPXG3Q==}
     engines: {node: '>=10'}
@@ -7081,6 +7115,10 @@ packages:
     dependencies:
       harmony-reflect: 1.6.2
 
+  /[email protected]:
+    resolution: {integrity: 
sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA==}
+    dev: false
+
   /[email protected]:
     resolution: {integrity: 
sha512-MAb38BcSbH0eHNBxn7ql2NH/kX33OkB3lZ1BNdh7ENeRChHTYsTvWrMubiIAMNS2llXEEgZ1MUOBtXChP3kaFQ==}
     engines: {node: '>= 4'}
@@ -7232,6 +7270,11 @@ packages:
     hasBin: true
     dev: true
 
+  /[email protected]:
+    resolution: {integrity: 
sha512-5BMULNob1vgFX6EjQw5izWDxrecWK9AM72rugNr0TFldMOi0fj6Jk+zeKIt0xGj4cEfQIJth4w3OKWOJ4f+AFw==}
+    engines: {node: '>=0.10.0'}
+    dev: false
+
   /[email protected]:
     resolution: {integrity: 
sha512-SbKbANkN603Vi4jEZv49LeVJMn4yGwsbzZworEoyEiutsN3nJYdbO36zfhGJ6QEDpOZIFkDtnq5JRxmvl3jsoQ==}
     engines: {node: '>=0.10.0'}
@@ -10530,6 +10573,14 @@ packages:
       ajv-formats: 2.1.1([email protected])
       ajv-keywords: 5.1.0([email protected])
 
+  /[email protected]:
+    resolution: {integrity: 
sha512-vfD3pmTzGpufjScBh50YHKzEu2lxBWhVEHsNGoEXmCmn2hKGfeNLYMzCJpe8cD7gqX7TJluOVpBkAequ6dgMmA==}
+    engines: {node: '>=4'}
+    dependencies:
+      extend-shallow: 2.0.1
+      kind-of: 6.0.3
+    dev: false
+
   /[email protected]:
     resolution: {integrity: 
sha512-mEugaLK+YfkijB4fx0e6kImuJdCIt2LxCRcbEYPqRGCs4F2ogyfZU5IAZRdjCP8JPq2AtdNoC/Dux63d9Kiryg==}
 
@@ -10999,6 +11050,11 @@ packages:
     dependencies:
       ansi-regex: 6.0.1
 
+  /[email protected]:
+    resolution: {integrity: 
sha512-uCC2VHvQRYu+lMh4My/sFNmF2klFymLX1wHJeXnbEJERpV/ZsVuonzerjfrGpIGF7LBVa1O7i9kjiWvJiFck8g==}
+    engines: {node: '>=0.10.0'}
+    dev: false
+
   /[email protected]:
     resolution: {integrity: 
sha512-vavAMRXOgBVNF6nyEEmL3DBK19iRpDcoIwW+swQ+CbGiu7lju6t+JklA1MHweoWtadgt4ISVUsXLyDq34ddcwA==}
     engines: {node: '>=4'}
diff --git a/ui/src/index.tsx b/ui/src/index.tsx
index e2930c08..174677cb 100644
--- a/ui/src/index.tsx
+++ b/ui/src/index.tsx
@@ -17,6 +17,8 @@
  * under the License.
  */
 
+import { Buffer } from 'buffer';
+
 import React from 'react';
 
 import ReactDOM from 'react-dom/client';
@@ -29,6 +31,8 @@ const root = ReactDOM.createRoot(
   document.getElementById('root') as HTMLElement,
 );
 
+window.Buffer = window.Buffer || Buffer;
+
 /**
  * Uniformly hide broken images
  *  - The `onload` event for elements such as `img` can only be `capture` on 
`document` (window cannot).
diff --git a/ui/src/pages/Questions/Ask/index.tsx 
b/ui/src/pages/Questions/Ask/index.tsx
index fb3b46ae..ef4b3d63 100644
--- a/ui/src/pages/Questions/Ask/index.tsx
+++ b/ui/src/pages/Questions/Ask/index.tsx
@@ -26,6 +26,7 @@ import dayjs from 'dayjs';
 import classNames from 'classnames';
 import isEqual from 'lodash/isEqual';
 import debounce from 'lodash/debounce';
+import matter from 'gray-matter';
 
 import { usePageTags, usePromptWithUnload } from '@/hooks';
 import { Editor, EditorRef, TagSelector } from '@/components';
@@ -113,14 +114,10 @@ const Ask = () => {
   const { qid } = useParams();
   const navigate = useNavigate();
   const [searchParams] = useSearchParams();
-  const initQueryTags = () => {
-    const queryTags = searchParams.get('tags');
-    if (!queryTags) {
-      return;
-    }
-    getTagsBySlugName(queryTags).then((tags) => {
+  const updateTags = (tags: string) => {
+    getTagsBySlugName(tags).then((resp) => {
       // eslint-disable-next-line
-      handleTagsChange(tags);
+      handleTagsChange(resp);
     });
   };
 
@@ -138,20 +135,27 @@ const Ask = () => {
   useEffect(() => {
     if (!qid) {
       // order: 1. tags query. 2. prefill query. 3. draft
-      initQueryTags();
-      let draft;
-      const prefill = searchParams.get('prefill');
-      if (prefill) {
-        draft = JSON.parse(decodeURIComponent(prefill));
-      } else {
-        draft = storageExpires.get(DRAFT_QUESTION_STORAGE_KEY);
+      const queryTags = searchParams.get('tags');
+      if (queryTags) {
+        updateTags(queryTags);
       }
+      const draft = storageExpires.get(DRAFT_QUESTION_STORAGE_KEY);
 
-      if (draft) {
-        formData.title.value = draft.title;
-        formData.content.value = draft.content;
-        formData.tags.value = draft.tags;
-        formData.answer_content.value = draft.answer_content;
+      const prefill = searchParams.get('prefill');
+      if (prefill || draft) {
+        if (prefill) {
+          const file = matter(decodeURIComponent(prefill));
+          formData.title.value = file.data?.title;
+          formData.content.value = file.content;
+          if (!queryTags && file.data?.tags) {
+            updateTags(file.data.tags);
+          }
+        } else if (draft) {
+          formData.title.value = draft.title;
+          formData.content.value = draft.content;
+          formData.tags.value = draft.tags;
+          formData.answer_content.value = draft.answer_content;
+        }
         setCheckState(Boolean(draft.answer_content));
         setHasDraft(true);
         setFormData({ ...formData });

Reply via email to