- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用带有插件的 gatsby gatsby-source-filesystem
和 gatsby-transformer-remark
将 markdown 文件显示为页面,如 official docs 中所述.
它工作得很好,但我正在寻找一种方法来向所有从 Markdown 转换的元素添加默认类。
假设我想要每个 <h1>
具有 title
类的元素, 和 <h2>
具有 subtitle
类的元素默认情况下。
我设法用 gatsby-remark-attr
做这样的事情,但这样我只能在 Markdown 文件中以编程方式添加类。它看起来像这样:
# My markdown heading
{.title}
## Subtitle
{.subtitle}
<h1 class="title">My markdown heading</h1>
<h2 class="subtitle">Subtitle</h2>
最佳答案
TL,博士:使用 gatsby-remark-default-html-attrs
Gatsby 的 gatsby-transformer-remark
使用 mdast-util-to-hast
将 markdown 节点转换为 html 节点,然后将其字符串化为原始 HTML。如果markdown节点有data.hProperties
对象,它将被转换为 html 属性。
假设您要添加类名 foo
给所有人 h1
节点。你需要:
h1
的 markdown 节点html 元素 data.hProperties
transformer-remark
的markdown节点.值得庆幸的是,使用 gatsby 创建本地插件很简单:
# Create a `plugins` folder at your root
mkdir plugins
mkdir plugins/remark-default-class-name
cd plugins/remark-default-class-name
npm init -y
touch index.js
root
|--src
|--gatsby-config.js
`--plugins
`--remark-default-class-name
|--package.json
`--index.js
gatsby-config.js
:
// gatsby-config.js
module.exports = {
plugins: [
{
resolve: `gatsby-transformer-remark`,
options: {
plugins: [
+ `remark-default-class-name`
],
},
},
markdownAST
对象,它允许您查找和修改节点。
unist-util-select
以帮助找到正确的节点。它带有
gatsby-transformer-remark
,但如果由于某些原因它不起作用,只需重新安装它。
const { selectAll } = require('unist-util-select');
module.exports = ({ markdownAST }) => {
// `heading` is equivalent to `h1...h6` in markdown.
// specify [depth] allow us to target the right heading tag.
const h1Nodes = selectAll('heading[depth=1]', markdownAST);
console.log(h1Nodes)
// this yields
// [{ type: "heading", children: [{ type: "text", value: "..." }] }, ...]
}
const h1Nodes = selectAll('heading[depth=1]', markdownAST);
- console.log(h1Nodes)
// node doesn't always have data
+ if (!node.data) node.data = {};
+ node.data.hProperties = {
+ className: 'foo'
+ }
h1
应该有一个
foo
现在上课。
remark
提供动力;所以谢谢你。
关于gatsby - 配置 gatsby-transformer-remark 添加默认类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54479917/
我有一段这样的代码。我发现 myResults = writer.getBuffer().toString(); 对某些用例返回 EMPTY STRING,但对其他用例则不返回。 我查看了服务器,但在
如何使用 javascript 通过 id 更改元素中的 -webkit-transform 、-moz-transform 、-o-transform 和 -ms-transform css? 这段
我正在使用 javax.xml.transform.Transformer.transform() 通过 xsl 样式表将一个 xml 转换为另一个 xml。我想以编程方式设置第一级 child 的排
为了使 seaborn.pairplot() 正常工作,在 jupyter notebook 中执行了以下步骤。 /usr/local/lib/python2.7/site-packages/matp
假设这个输入 XML 编写这些代码行: StreamSource source = new StreamSource(new StringReader(/* the above XML*/));
如何在 spring 框架中配置 java.xml.transform.Transformer ?我需要转换器的实例来通过 xslt 将 xml 转换为文本。因此,配置的转换器应该了解 xslt 样式
我一直在核心数据中使用可转换属性,将图像和颜色等复杂对象转换为原始数据。我拿了this ... The idea behind transformable attributes is that you
我正在尝试打开 XML 文件,添加一些更改,然后保存到其他 XML 文件结果。我正在使用标准 javax.xml.parsers.* 和 javax.xml.transform* 类。 但在保存的文档
Transformer(变换方法)对输入源的大小有限制吗? 我正在尝试转换一个相当长的 (18M) XML,但收到一个奇怪的错误 "The element type "HR" must be term
我正在尝试解析一个非常简单的示例: 100 我使用的样式表如下: 这在 libxs
来自文档 for from_pretrained ,我知道我不必每次都下载预训练的向量,我可以使用以下语法保存它们并从磁盘加载: - a path to a `directory` contain
默认缓存目录磁盘容量不足,我需要更改默认缓存目录的配置。 最佳答案 您可以在每次加载模型时指定缓存目录 .from_pretrained通过设置参数cache_dir .您可以通过导出环境变量 TRA
有一个函数,例如: CATransform3DGetAffineTransform Returns the affine transform represented by 't'. If 't' ca
我有一个包含 WCF 设置的配置文件: “add”元素只有一个 baseAddress 属性,所以我不能使用 Match 定位器。一种方法如何像我的示例中那样转换多个元素? 最
在收到下面链接中描述的错误后,我已将实体属性的 Transfomer 设置为 NSSecureUnarchiveFromData(之前为 nil)。 CoreData crash error Xcod
当我写Document时使用 Transformer 的 transform() 方法转换为 XML,生成的 XML 文档的格式很好 - 所有元素都写在单独的行上并缩进。除了第一个元素与定义写在同一行
我不明白 StreamResult 实例会发生什么。我看到 Transformer 对象接收 source 和 streamResult: transformer.transform(sour
从下面的代码片段我应该得出结论,std::transform 比 boost::transform 更受欢迎,因为前者使用更少的初始化和析构函数可能更有效比后者? #include #include
transform() 可以将函数应用到序列的元素上,并将这个函数返回的值保存到另一个序列中,它返回的迭代器指向输出序列所保存的最后一个元素的下一个位置。 这个算法有一个版本和 for_each()
我是 react-native 的新手。在项目上将 react-native 从 0.48.3 升级到 0.62.2 后,运行“react-native run-ios”命令时出现错误:“index.
我是一名优秀的程序员,十分优秀!