Microsoft ClearScript 中匿名类型的“语法错误:速记属性初始值设定项无效”

'Syntaxerror: invalid shorthand property initializer' for anonymous type in Microsoft ClearScript

提问人:ElektroStudios 提问时间:7/7/2023 最后编辑:ElektroStudios 更新时间:7/7/2023 访问量:34

问:

我想为 markdown-it JavaScript 库创建一个基本的 .NET 包装器(请参阅此处markdown-it.js 源代码)。为了实现这一点,我使用了 Microsoft ClearScript

请注意,我对 JavaScript 的了解非常有限,这只是我尝试集成到 .NET 中的第二个 JavaScript 库。因此,同样,我对Microsoft的ClearScript的经验也很有限。

井。经过简单的研究,我了解到,当我们使用等于运算符 (=) 而不是冒号 (:) 来分隔对象中的键值(属性)时,会出现 JavaScript 中的错误,这没关系,但问题是:当尝试在 Microsoft ClearScript 中创建和使用这种键值(属性)对象时,如何解决问题?Invalid shorthand property initializer

这是我所拥有的:

Public Shared Function ConvertMarkdownToHtml(str As String,
                                             Optional presetName As String = Nothing,
                                             Optional options As Object = Nothing) As String

    ' I set these values just to test the issue.
    presetName = "default"
    options = New With {.linkify = True}

    Dim htmlOutput As String
    Using engine As New V8ScriptEngine("markdownit_engine", V8ScriptEngineFlags.DisableGlobalMembers)

        ' https://raw.githubusercontent.com/markdown-it/markdown-it/master/dist/markdown-it.js
        Dim markdownItCode As String = My.Resources.markdown_it
        engine.Execute(markdownItCode)
        engine.Execute($"md = new markdownit('{presetName}', {options});
                         var renderResult = md.render('{str}');")

        htmlOutput = CStr(engine.Evaluate("renderResult"))

        engine.CollectGarbage(exhaustive:=True)
    End Using

    Return htmlOutput
End Function

正如您在上面的代码中看到的,我正在使用匿名类型来创建对象。我认为该对象被发送并转换为带有“=”分隔符而不是“:”的函数,但我不知道在这种情况下如何解决这个问题。optionsrender


最后,如果有帮助的话,函数文档是这样说的:render

* new MarkdownIt([presetName, options])
* - presetName (String): optional, `commonmark` / `zero`
* - options (Object)
*
* Creates parser instanse with given config. Can be called without `new`.
*
* ##### presetName
*
* MarkdownIt provides named presets as a convenience to quickly
* enable/disable active syntax rules and options for common use cases.
*
* - ["commonmark"](https://github.com/markdown-it/markdown-it/blob/master/lib/presets/commonmark.js) -
*   configures parser to strict [CommonMark](http://commonmark.org/) mode.
* - [default](https://github.com/markdown-it/markdown-it/blob/master/lib/presets/default.js) -
*   similar to GFM, used when no preset name given. Enables all available rules,
*   but still without html, typographer & autolinker.
* - ["zero"](https://github.com/markdown-it/markdown-it/blob/master/lib/presets/zero.js) -
*   all rules disabled. Useful to quickly setup your config via `.enable()`.
*   For example, when you need only `bold` and `italic` markup and nothing else.
*
* ##### options:
*
* - __html__ - `false`. Set `true` to enable HTML tags in source. Be careful!
*   That's not safe! You may need external sanitizer to protect output from XSS.
*   It's better to extend features via plugins, instead of enabling HTML.
* - __xhtmlOut__ - `false`. Set `true` to add '/' when closing single tags
*   (`<br />`). This is needed only for full CommonMark compatibility. In real
*   world you will need HTML output.
* - __breaks__ - `false`. Set `true` to convert `\n` in paragraphs into `<br>`.
* - __langPrefix__ - `language-`. CSS language class prefix for fenced blocks.
*   Can be useful for external highlighters.
* - __linkify__ - `false`. Set `true` to autoconvert URL-like text to links.
* - __typographer__  - `false`. Set `true` to enable [some language-neutral
*   replacement](https://github.com/markdown-it/markdown-it/blob/master/lib/rules_core/replacements.js) +
*   quotes beautification (smartquotes).
* - __quotes__ - `“”‘’`, String or Array. Double + single quotes replacement
*   pairs, when typographer enabled and smartquotes on. For example, you can
*   use `'«»„“'` for Russian, `'„“‚‘'` for German, and
*   `['«\xA0', '\xA0»', '‹\xA0', '\xA0›']` for French (including nbsp).
* - __highlight__ - `null`. Highlighter function for fenced code blocks.
*   Highlighter `function (str, lang)` should return escaped HTML. It can also
*   return empty string if the source was not changed and should be escaped
*   externaly. If result starts with <pre... internal wrapper is skipped.
*
* ##### Example
*
* ```javascript
* // commonmark mode
* var md = require('markdown-it')('commonmark');
*
* // default mode
* var md = require('markdown-it')();
*
* // enable everything
* var md = require('markdown-it')({
*   html: true,
*   linkify: true,
*   typographer: true
* });
* ```

更新

我发现了一种解决此问题的肮脏方法,通过将匿名类型转换为字符串并进行一些替换:

Public Class MarkdownItOptions

    Public Sub New()
    End Sub

    Public Property Html As Boolean
    Public Property XhtmlOutput As Boolean
    Public Property BreakLines As Boolean
    Public Property LangPrefix As String = "language-"
    Public Property Linkify As Boolean
    Public Property Typographer As Boolean
    Public Property Quotes As String = "“”‘’"

    ''' <summary>
    ''' Builds the 'options' object to pass to the Markdown-It's 'render' function.
    ''' </summary>
    Protected Friend Function BuildMarkdownItOptionsObject() As String
        Dim options As New With {
            .html = Me.Html,
            .xhtmlOut = Me.XhtmlOutput,
            .breaks = Me.BreakLines,
            .langPrefix = $"'{Me.LangPrefix}'",
            .linkify = Me.Linkify,
            .typographer = Me.Typographer,
            .quotes = $"'{Me.Quotes}'"
        }

        Return options.ToString().Replace(" =", ":"c).
                                  Replace(": True", ": true").
                                  Replace(": False", ": false")
    End Function

End Class

此方法适用于此特定情况,但手动执行字符串替换似乎不安全,当属性值不同且不可转换为字符串时,它可能不适用于其他情况。因此,我保持这个问题的开放性,以寻找适当的解决方案。

JavaScript .NET vb.net ClearScript

评论


答: 暂无答案