summaryrefslogtreecommitdiffstatshomepage
path: root/scripts/generate-web-types.mjs
blob: 34b4b6616c2677542fea88146834b7b95aeba871 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import fs from 'fs'

const classes = []
const attributes = []
const events = []

const rootPath = fs.existsSync('./www') ? './' : '../'

for (const file of fs.readdirSync(rootPath + 'www/content/attributes').sort()) {
  if (file.startsWith('hx-') && file.endsWith('.md')) {
    const name = file.slice(0, -3)
    const info = readAttributeInfo(name, rootPath + 'www/content/attributes/' + file)
    attributes.push({
      name,
      ...info,
      'doc-url': 'https://htmx.org/attributes/' + name + '/'
    })
  }
}

readClassInfo()
readEventInfo()

const pkg = JSON.parse(fs.readFileSync(rootPath + 'package.json', { encoding: 'utf8' }))

const webTypes = {
  $schema: 'https://json.schemastore.org/web-types',
  name: 'htmx',
  version: pkg.version,
  'default-icon': './htmx.svg',
  'js-types-syntax': 'typescript',
  'description-markup': 'markdown',
  contributions: {
    html: {
      attributes
    },
    css: {
      classes
    },
    js: {
      events: [
        {
          name: 'HTMX event',
          pattern: {
            items: ['/js/htmx-events'],
            template: ['htmx:', '$...', '#item:HTMX event']
          }
        }
      ],
      'htmx-events': events
    }
  }
}

fs.writeFileSync(rootPath + 'editors/jetbrains/htmx.web-types.json', JSON.stringify(webTypes, null, 2))

function readAttributeInfo(name, file) {
  const content = fs.readFileSync(file, { encoding: 'utf8' })

  const isInherited = content.indexOf('`' + name + '` is inherited') !== -1
  const isNotInherited = content.indexOf('`' + name + '` is not inherited') !== -1

  const deprecated = content.indexOf('`' + name + '` has been deprecated') !== -1

  const sections = {}

  if (isInherited) {
    sections.Inherited = ''
  } else if (isNotInherited) {
    sections['Not Inherited'] = ''
  }

  const descSections = /\+\+\+\n(?:[^\n]*\n)+\+\+\+\n\n((?:[^\n]+\n)+)(?:\n((?:[^\n]+\n)+))?(?:\n((?:[^\n]+\n)+))?/mg.exec(content)
  const para1 = descSections[1].trim()
  const para2 = descSections[2]?.trim()
  const para3 = descSections[3]?.trim()

  let description = para1
  if (para2) {
    description += '\n\n' + para2
  }
  if (para2 && para2.endsWith(':') && para3) {
    description += '\n\n' + para3
  }

  let pattern
  if (name === 'hx-on') {
    pattern = {
      or: [
        {
          items: ['/js/events'],
          template: ['hx-on:', '#...', '#item:JS event']
        },
        {
          items: ['/js/htmx-events'],
          template: ['hx-on::', '#...', '#item:HTMX event']
        }
      ]
    }
  }

  return {
    pattern,
    description,
    'description-sections': sections,
    deprecated: deprecated ? true : undefined
  }
}

function readClassInfo() {
  const content = fs.readFileSync(rootPath + 'www/content/reference.md', { encoding: 'utf8' })
  const start = content.indexOf('| Class | Description |')
  const cssTable = content.slice(start, content.indexOf('</div>', start))
  const expr = /\| `([^`]+)` \| ([^\n]+)/mg
  let match = expr.exec(cssTable)
  while (match) {
    const name = match[1]
    if (name && name.startsWith('htmx-')) {
      classes.push({
        name,
        description: match[2].trim(),
        'doc-url': 'https://htmx.org/reference/#classes'
      })
    }
    match = expr.exec(cssTable)
  }
}

function readEventInfo() {
  const content = fs.readFileSync(rootPath + 'www/content/events.md', { encoding: 'utf8' })
  const expr = /### Event - `([^`]+)`[^\n]*\n+((?:(?:[^#\n]|#####)[^\n]*\n+)+)/mg
  let match = expr.exec(content)
  while (match) {
    let name = match[1]
    if (name && name.startsWith('htmx:')) {
      name = name.slice(5)
      events.push({
        name,
        description: match[2],
        'doc-url': 'https://htmx.org/events/#htmx:' + name
      })
    }
    match = expr.exec(content)
  }
}