You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

寻求类Ruby风格的浏览器端JavaScript DSL解决方案

问题描述

我非常喜欢用Ruby编写领域特定语言(DSL),它的语法比我试过的其他语言写的DSL更简洁易读。现在我想把大量DSL迁移到浏览器环境,这就需要能在JavaScript中运行的DSL。我觉得用原生TypeScript或JavaScript写DSL的可读性不如Ruby,但也可能是我想错了。我想知道有没有适合编写DSL、还能转译为JavaScript的语言,或者可以生成JavaScript超集的高阶语言AST。我已经写过很多遵循Builder(包含Director)设计模式的DSL,下面是用DSL构建DrawIO图的示例:

Ruby DSL示例代码

DrawioDsl::Drawio
  .diagram(theme: :style_11)
  .page('Style-Plain', margin_left: 0, margin_top: 0, rounded: 0, background: '#FFEADB') do
    grid_layout(wrap_at: 8)

    interface(theme: :style_07) do
      format
        .header('IPerson')
        .field(:field1, type: :string)
        .field(:field2, type: :string)
        .method(:full_name, type: :string)
    end

    klass do
      format
        .header('Person')
        .field(:field1, type: :string)
        .field(:field2, type: :string)
        .field(:age, type: :integer)
        .field(:birthday, type: :date)
        .method(:full_name, type: :string)
    end

    klass(h: 280) do
      format
        .header('Custom HTML')
        .p('David')
        .b('Cruwys')
        .hr
        .h1('h1')
        .h2('h2')
        .h3('h3')
        .h4('h4')
        .h5('h5')
        .h6('h6')
        .ul_s
        .li('li')
        .li('li')
        .ul_e
    end
  end
end

生成的DrawIO示例图

DrawIO示例图

内容的提问来源于stack exchange,提问作者David Cruwys

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 23:55:21