Closed
Description
你在什么场景下需要该功能?
在前端里,分享设计与代码是很酷的玩法,国外有codepen和jsfiddle,国内目前很爽的方案有jsrun.net。通过代码的分享在线渲染会提高可玩性,促进代码水平的提高。
描述最优的解决方案
为了减少学习成本和实现难度,结合jsrun.net给的内嵌选项,有下面的思考解决方案
- 使用代码块声明jsrun
```jsrun
// 采用toml结构实现单一项配置,也可以通过vditor实现全局的配置,单一配置的优先级更高
[config]
theme = "dark" or "light"
src ="分享的demo地址(忽略http协议类型)"
type = "分享的仓库类型"
height = "输出的iframe的高度,默认为300"
\`\`\`
- 举个栗子,demo地址为 http://jsrun.net/G46Kp/edit
[config]
theme = "dark"
src = "jsrun.net/G46Kp"
type = "web"
- 输出结果为
<iframe width="100%" height="300" src="//jsrun.net/G46Kp/embedded/all/dark" allowfullscreen="allowfullscreen" frameborder="0"></iframe>
- 如果主题是light的话
<iframe width="100%" height="300" src="//jsrun.net/G46Kp/embedded/all/light" allowfullscreen="allowfullscreen" frameborder="0"></iframe>
默认允许所有的tabs存在
- 如果类型是JavaScript,demo的地址为 http://js.jsrun.net/c2aKp/edit
[config]
theme = "dark"
src = "js.jsrun.net/c2aKp"
type = "js"
- 输出结果
<iframe width="100%" height="300" src="//js.jsrun.net/c2aKp/embedded/quick/dark" allowfullscreen="allowfullscreen" frameborder="0"></iframe>
默认使用极速运行
描述候选解决方案
与上面同理, codepen等其他的也可以通过配置文件的方式来进行配置,文件格式改为下面那样
[config]
engine = "使用的engine,codepen | jsrun | jsfiddle等"
theme = "可选主题"
src="分享的地址"
height = "可修改的iframe高度"
type = "分享的语言类型"
Activity
Vanessa219 commentedon Jan 25, 2021
看上去好像是直接嵌入一个 iframe 就可以了?
HerbertHe commentedon Jan 26, 2021
确实是的😂,我之前看react native官网也是通过iframe实现的
HerbertHe commentedon Jan 26, 2021
补充举例codepen
其格式为
如果是codepen的话,可以将配置文件设成下面这样
配置文件语言类型使用保留字
vditorCodeShare
,而非之前单独举例的jsrun
Vanessa219 commentedon Jan 27, 2021
vditor 已支持 iframe 嵌入,直接输入就可以哦。
HerbertHe commentedon Jan 27, 2021
嗷嗷,谢谢
HerbertHe commentedon Jan 27, 2021
@Vanessa219 不过好像还是有一个问题,如果用户用iframe插入广告这不是不可控了吗
Vanessa219 commentedon Jan 28, 2021
那可能需要自己建立一个白名单或者黑名单
HerbertHe commentedon Jan 28, 2021
😭那也行