默认情况下,模板引擎仅在特定的文件类型列表中支持这些条件运算符,有时具有不同的语法。您可以找到该文件列表在协调器的源代码中 https://github.com/dotnet/templating/blob/8986b68/src/Microsoft.TemplateEngine.Orchestrator.RunnableProjects/SimpleConfigModel.cs#L387-L464。截至目前,该列表不包括 Markdown 文件,这就是为什么您没有在那里获得任何功能的原因。
幸运的是,似乎有一种配置方法特殊定制操作 https://github.com/dotnet/templating/wiki/%22Runnable-Project%22-Templates#customoperations-optional-and-specialcustomoperations-optional内的自定义文件类型template.json
,它允许您定义自定义操作 https://github.com/dotnet/templating/wiki/%22Runnable-Project%22-Templates#customoperationsoperations-optional例如对于条件运算符。
添加这样的东西应该可以工作:
"SpecialCustomOperations": {
"**/*.md": {
"operations": [
{
"type": "conditional",
"configuration": {
"if": ["---#if"],
"else": ["---#else"],
"elseif": ["---#elseif", "---#elif"],
"endif": ["---#endif"],
"trim" : "true",
"wholeLine": "true",
}
}
]
}
}
它应该允许您在您的.md
files:
# This is an example Markdown
---#if (FooBar)
Foo bar
---#elif (BarBaz)
Bar baz
---#else
Baz qux
---#endif
请注意,我在这里使用了不同的语法,因为基于单行的语法更容易配置。