Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 在react js中使用ReactMarkdown时,不会呈现图像(<;img)标记_Reactjs_Markdown_Jsx - Fatal编程技术网

Reactjs 在react js中使用ReactMarkdown时,不会呈现图像(<;img)标记

Reactjs 在react js中使用ReactMarkdown时,不会呈现图像(<;img)标记,reactjs,markdown,jsx,Reactjs,Markdown,Jsx,目前,我正在尝试获取github自述文件并使用ReactMarkdown呈现它。是输出/错误的屏幕截图 有一些标签像 <p float="left"> <img src="https://github.com/username/project/blob/master/Screenshots/s1.png" width="300" hspace="40"/> </p> 所以上面

目前,我正在尝试获取github自述文件并使用ReactMarkdown呈现它。是输出/错误的屏幕截图

有一些标签像

<p float="left"> <img src="https://github.com/username/project/blob/master/Screenshots/s1.png" width="300" hspace="40"/> </p>

所以上面的标记不会呈现,并给出CORB错误

我现在的代码是

 <ReactMarkdown
          className="projectDetail__markdown"
          escapeHtml={false}
          transformImageUri={(uri) =>
            uri.startsWith("https")
              ? uri
              : `https://raw.githubusercontent.com/AlShevelev/WizardCamera/master/screenshots/5_2_small.webp`
          }
          source={markDown}
          // astPlugins={[parseHtml]}
          renderers={{ code: CodeBlock }}
        </ReactMarkdown>


uri.startsWith(“https”)
? uri
: `https://raw.githubusercontent.com/AlShevelev/WizardCamera/master/screenshots/5_2_small.webp`
}
source={markDown}
//astPlugins={[parseHtml]}
渲染器={code:CodeBlock}

我尝试过使用插件,但没有成功。

最终我找到了解决方案

我在控制台中看到了CORB错误,我研究了为什么会发生这种情况,并发现自述文件中图像的url不正确

自述文件url被删除&& 所需的Url为:

所以问题是,当我们为图像设置src时,我们需要使用一个指向实际图像的URL,而第一个URL没有指向实际图像

这是根本原因,因此图像无法渲染

因此,我编写代码来转换img标记的所有URL,只转换标记响应的URL。 完整的代码

// This function will find all the links in img tag.

function getImages(string) {
  const imgRex = /<img.*?src="(.*?)"[^>]+>/g;
  const images = [];
  let img;
  while ((img = imgRex.exec(string))) {
    images.push(img[1]);
  }
  return images;
}

// This function convert the markdown text. 

const convertImgInMarkdown = (markDown) => {
  let mark = markDown;
  let imageTags = getImages(mark);
  let updatedImages = [];
  imageTags.map((image) => {
    let xx = image.split(".com");
    let y = `https://raw.githubusercontent.com` + xx[1];
    let z = y.replace("/blob", "");
    updatedImages.push(z);
  });
  for (let i = 0; i < updatedImages.length; i++) {
    mark = mark.replace(imageTags[i], updatedImages[i]);
  }
  return mark;
};
//此函数将查找img标记中的所有链接。
函数getImages(字符串){
常量imgRex=/]+>/g;
常量图像=[];
让img;
while((img=imgRex.exec(string))){
images.push(img[1]);
}
返回图像;
}
//此函数用于转换标记文本。
const convertImgInMarkdown=(markDown)=>{
让马克=降价;
让imageTags=getImages(标记);
让updateImage=[];
imageTags.map((图像)=>{
设xx=image.split(“.com”);
设y=`https://raw.githubusercontent.com`+xx[1];
设z=y.replace(“/blob”,”);
UpdateImage.push(z);
});
for(设i=0;i