如何通过REST API发送Markdown,并在前端(React)中呈现它。

huangapple go评论83阅读模式
英文:

How to send markdown over a REST API, and render it in the frontend (React)

问题

我试图将一个Markdown文件转换并通过REST API发送,并在前端进行呈现。问题在于,在尝试转换回来时,换行符和其他内容会丢失。

如何以保留原始Markdown的方式呈现它?

是否有标准的方法来实现这一点?

尝试使用json.stringify来转换Markdown,通过API发送,然后尝试将其转换回Markdown,但不再像原始内容一样工作。

英文:

So I'm trying to convert a markdown file and send it over a rest API, and render it in the frontend. The problem is that the newlines, and other things get lost while trying to convert it back.

How do I render it back in a way that the original markdown is preserved?

Is there a standard way of doing this?

Tried to convert markdown by json.stringify, sent it over the api, tried to convert it back to markdown but doesn't work like original anymore.

答案1

得分: 1

你可以将所有内容放在一个 <pre> 标签中,如下所示:

<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
    <pre>
       This is 
       some code
       A json:
       {
         key: value
       }
    </pre>
<!-- end snippet -->

<pre> 标签中,文本会保留空格和换行,因此它将以与 HTML 源代码中编写的方式完全相同的方式显示。

英文:

You can add all the content in a &lt;pre&gt; tag as following

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;pre&gt;
   This is 
   some code
   A json:
   {
     key: value
   }
&lt;/pre&gt;

<!-- end snippet -->

In a &lt;pre&gt; tag, the text preserves both spaces and line breaks thus it will be displayed exactly as written in the HTML source code.

答案2

得分: 0

你可以将原始文件作为字符串发送,或者如果不行,可以使用这个函数格式化对象:

const object = {a:[15,3457,15,"afbsv",[4,34,36],{
  l: "dsfvszd",
  qwe: 238475463,
  iuggbsf: ["AEfs",],
  afafwwa:{afafwafaw:{r:"4"}}
}]}

document.write(`<pre>${format(object)}</pre>`);

function format(object) {
  let result = "";
  processObject(object, 2);
  
  function processObject(object, depth, isObjectValue = false, trailingComma = "") {
    if (Array.isArray(object)) {
      result += `${isObjectValue ? "&nbsp;" : "<br>" + "&nbsp;".repeat(depth - 2)}[`;

      for (let i = 0; i < object.length; i++) {
        const element = object[i],
          trailingComma = i + 1 === object.length ? "" : ",";
        
        switch (typeof element) {
          case "object":
            processObject(element, depth + 2, false, trailingComma);
            break;
          case "string":
            result += `<br>${"&nbsp;".repeat(depth)}"${element}"${trailingComma}`;
            break;
          case "number":
            result += `<br>${"&nbsp;".repeat(depth) + element}${trailingComma}`;
            break;
        }
      }

      result += `<br>${"&nbsp;".repeat(depth - 2)}]${trailingComma}`;
    } else {
      result += `${isObjectValue ? "&nbsp;" : "<br>" + "&nbsp;".repeat(depth - 2)}{`;
      
      let keyIndex = 0,
        keyCount = Object.keys(object).length;
      for (key in object) {
        const value = object[key],
          trailingComma = ++keyIndex === keyCount ? "" : ",";
        switch (typeof value) {
          case "object":
            result += `<br>${"&nbsp;".repeat(depth)}"${key}":`;
            processObject(value, depth + 2, true, trailingComma);
            break;
          case "string":
            result += `<br>${"&nbsp;".repeat(depth)}"${key}": "${value}"${trailingComma}`;
            break;
          case "number":
            result += `<br>${"&nbsp;".repeat(depth)}"${key}": ${value}${trailingComma}`;
            break;
        }
      }

      result += `<br>${"&nbsp;".repeat(depth - 2)}}${trailingComma}`;
    }
  }
  
  return result;
}

只需使用format(objectGoesHere),它会返回一个适用于HTML格式的字符串。

英文:

You can either send the original file as a string, or if that is not an option, you can format the object with this function:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const object = {a:[15,3457,15,&quot;afbsv&quot;,[4,34,36],{
l: &quot;dsfvszd&quot;,
qwe: 238475463,
iuggbsf: [&quot;AEfs&quot;,],
afafwwa:{afafwafaw:{r:&quot;4&quot;}}
}]}
document.write(`&lt;pre&gt;${format(object)}&lt;/pre&gt;`);
function format(object) {
let result = &quot;&quot;;
processObject(object, 2);
function processObject(object, depth, isObjectValue = false, trailingComma = &quot;&quot;) {
if (Array.isArray(object)) {
result += `${isObjectValue ? &quot;&amp;nbsp;&quot; : &quot;&lt;br&gt;&quot; + &quot;&amp;nbsp;&quot;.repeat(depth - 2)}[`;
for (let i = 0; i &lt; object.length; i++) {
const element = object[i],
trailingComma = i + 1 === object.length ? &quot;&quot; : &quot;,&quot;;
switch (typeof element) {
case &quot;object&quot;:
processObject(element, depth + 2, false, trailingComma);
break;
case &quot;string&quot;:
result += `&lt;br&gt;${&quot;&amp;nbsp;&quot;.repeat(depth)}&quot;${element}&quot;${trailingComma}`;
break;
case &quot;number&quot;:
result += `&lt;br&gt;${&quot;&amp;nbsp;&quot;.repeat(depth) + element}${trailingComma}`;
break;
}
}
result += `&lt;br&gt;${&quot;&amp;nbsp;&quot;.repeat(depth - 2)}]${trailingComma}`;
} else {
result += `${isObjectValue ? &quot;&amp;nbsp;&quot; : &quot;&lt;br&gt;&quot; + &quot;&amp;nbsp;&quot;.repeat(depth - 2)}{`;
let keyIndex = 0,
keyCount = Object.keys(object).length;
for (key in object) {
const value = object[key],
trailingComma = ++keyIndex === keyCount ? &quot;&quot; : &quot;,&quot;;
switch (typeof value) {
case &quot;object&quot;:
result += `&lt;br&gt;${&quot;&amp;nbsp;&quot;.repeat(depth)}&quot;${key}&quot;:`;
processObject(value, depth + 2, true, trailingComma);
break;
case &quot;string&quot;:
result += `&lt;br&gt;${&quot;&amp;nbsp;&quot;.repeat(depth)}&quot;${key}&quot;: &quot;${value}&quot;${trailingComma}`;
break;
case &quot;number&quot;:
result += `&lt;br&gt;${&quot;&amp;nbsp;&quot;.repeat(depth)}&quot;${key}&quot;: ${value}${trailingComma}`;
break;
}
}
result += `&lt;br&gt;${&quot;&amp;nbsp;&quot;.repeat(depth - 2)}}${trailingComma}`;
}
}
return result;
}

<!-- end snippet -->

Just use format(objectGoesHere) and it will return a string that is formatted for HTML.

答案3

得分: -1

你可以尝试使用 react-markdown

英文:

You can try using react-markdown

huangapple
  • 本文由 发表于 2023年2月23日 21:55:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75545756.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定