@magic/transmute 0.0.58 → 0.0.59

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magic/transmute",
3
- "version": "0.0.58",
3
+ "version": "0.0.59",
4
4
  "author": "Wizards & Witches",
5
5
  "homepage": "https://magic.github.io/transmute",
6
6
  "license": "AGPL-3.0",
@@ -36,13 +36,13 @@
36
36
  },
37
37
  "dependencies": {
38
38
  "@magic/cases": "0.0.13",
39
- "@magic/cli": "0.0.53",
39
+ "@magic/cli": "0.0.54",
40
40
  "@magic/entities": "0.0.17",
41
41
  "@magic/log": "0.1.22",
42
42
  "@magic/types": "0.1.37",
43
43
  "marked": "18.0.5",
44
44
  "posthtml-parser": "0.12.1",
45
- "prettier": "3.8.3"
45
+ "prettier": "3.8.5"
46
46
  },
47
47
  "devDependencies": {
48
48
  "@magic-modules/git-badges": "0.0.12",
@@ -51,7 +51,7 @@
51
51
  "@magic-modules/pre": "0.0.12",
52
52
  "@magic-themes/docs": "0.0.15",
53
53
  "@magic/core": "0.0.158",
54
- "@magic/format": "0.0.73",
54
+ "@magic/format": "0.0.74",
55
55
  "@magic/test": "0.3.12"
56
56
  },
57
57
  "keywords": [
package/src/bin/index.js CHANGED
@@ -1,2 +1,2 @@
1
- export { handleArgv } from './handleArgv.mjs'
2
- export { runCli } from './runCli.mjs'
1
+ export { handleArgv } from './handleArgv.js'
2
+ export { runCli } from './runCli.js'
package/src/bin/runCli.js CHANGED
@@ -6,7 +6,7 @@ import prettier from 'prettier'
6
6
  import log from '@magic/log'
7
7
  import is from '@magic/types'
8
8
 
9
- import transmute from '../index.mjs'
9
+ import transmute from '../index.js'
10
10
 
11
11
  export const runCli = (html, isMarkdown, res) => {
12
12
  let out
package/src/bin.js CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  import { cli } from '@magic/cli'
4
4
 
5
- import { handleArgv, runCli } from './bin/index.mjs'
5
+ import { handleArgv, runCli } from './bin/index.js'
6
6
 
7
7
  const args = {
8
8
  options: [
@@ -41,13 +41,13 @@ transpile markdown string:
41
41
  magic-transmute markdown --str '[a link](https://example.com)'',
42
42
 
43
43
  transpile html file:
44
- magic-transmute file --input input.html --output output.mjs
44
+ magic-transmute file --input input.html --output output.js
45
45
 
46
46
  transpile markdown file (.markdown and .md get recognized):
47
- magic-transmute file --input input.md --output output.mjs
47
+ magic-transmute file --input input.md --output output.js
48
48
 
49
49
  transpile file as markdown (arbitrary file extensions)
50
- magic-transmute file --input input.txt --output output.mjs --markdown
50
+ magic-transmute file --input input.txt --output output.js --markdown
51
51
  `,
52
52
  },
53
53
  }
package/src/index.js CHANGED
@@ -3,9 +3,9 @@ import { parser } from 'posthtml-parser'
3
3
 
4
4
  import is from '@magic/types'
5
5
 
6
- import { findState, implantState } from './lib/index.mjs'
6
+ import { findState, implantState } from './lib/index.js'
7
7
 
8
- import * as renderers from './renderers/index.mjs'
8
+ import * as renderers from './renderers/index.js'
9
9
 
10
10
  export const markdown = (string, state = {}, originalState = {}) => {
11
11
  const { input, state: st = {} } = findState(string)
@@ -2,21 +2,37 @@ import { Renderer } from 'marked'
2
2
 
3
3
  export const markdown = new Renderer()
4
4
 
5
+ // Slugger implementation (removed from marked v5+)
6
+ // No deduplication - reset state for each slugify call
7
+ const slugify = str => {
8
+ let s = str
9
+ .toLowerCase()
10
+ .trim()
11
+ .replace(/[\s]+/g, '-')
12
+ .replace(/[^\w-]+/g, '')
13
+ .replace(/--+/g, '-')
14
+ .replace(/^-+/, '')
15
+ .replace(/-+$/, '')
16
+ return s
17
+ }
18
+
5
19
  /* overwrite builtin markdown functions to use @magic modules */
6
20
 
7
- markdown.code = (code, infostring) => {
8
- let lang = (infostring || '').match(/\S*/)[0]
21
+ markdown.code = token => {
22
+ const { text, lang } = token
23
+ const language = (lang || '').match(/\S*/)[0]
9
24
 
10
- if (!lang) {
11
- return `<Pre>${code}</Pre>\n`
25
+ if (!language) {
26
+ return `<Pre>${text}</Pre>\n`
12
27
  }
13
28
 
14
- return `<Pre class="${lang}">${code}</Pre>`
29
+ return `<Pre class="${language}">${text}</Pre>`
15
30
  }
16
31
 
17
- markdown.codespan = text => `<Pre lines="false">${text}</Pre>`
32
+ markdown.codespan = token => `<Pre lines="false">${token.text}</Pre>`
18
33
 
19
- markdown.link = (href, title, text) => {
34
+ markdown.link = token => {
35
+ const { href, title, text } = token
20
36
  if (href === null) {
21
37
  return text
22
38
  }
@@ -36,7 +52,8 @@ markdown.link = (href, title, text) => {
36
52
  return out
37
53
  }
38
54
 
39
- markdown.image = (href, title, text) => {
55
+ markdown.image = token => {
56
+ const { href, title, text } = token
40
57
  if (href === null) {
41
58
  return text
42
59
  }
@@ -56,12 +73,13 @@ markdown.image = (href, title, text) => {
56
73
  return out
57
74
  }
58
75
 
59
- markdown.heading = (text, level, raw, slugger) => {
60
- let id = slugger.slug(raw)
76
+ markdown.heading = token => {
77
+ let { text, depth } = token
78
+ let id = slugify(text)
61
79
  let cssClass = ''
62
80
 
63
- if (raw.startsWith('#')) {
64
- let [setId, ...t] = raw.split(' ')
81
+ if (text.startsWith('#')) {
82
+ let [setId, ...t] = text.split(' ')
65
83
 
66
84
  text = t.join(' ')
67
85
 
@@ -69,11 +87,11 @@ markdown.heading = (text, level, raw, slugger) => {
69
87
  setId += text
70
88
  }
71
89
 
72
- id = slugger.slug(setId.substr(1))
90
+ id = slugify(setId.substr(1))
73
91
  }
74
92
 
75
93
  if (text.startsWith('.')) {
76
- let [setClass, ...t] = raw.split(' ')
94
+ let [setClass, ...t] = text.split(' ')
77
95
 
78
96
  text = t.join(' ')
79
97
 
@@ -81,13 +99,14 @@ markdown.heading = (text, level, raw, slugger) => {
81
99
  setClass += text
82
100
  }
83
101
 
84
- cssClass = ` class="${slugger.slug(setClass)}"`
102
+ cssClass = ` class="${slugify(setClass)}"`
85
103
  }
86
104
 
87
- return `<h${level} id="${id}"${cssClass}>${text}</h${level}>\n`
105
+ return `<h${depth} id="${id}"${cssClass}>${text}</h${depth}>\n`
88
106
  }
89
107
 
90
- markdown.html = html => {
108
+ markdown.html = token => {
109
+ let html = token.text || token.raw
91
110
  if (html.startsWith('<img')) {
92
111
  html = html.replace('<img', '<Img')
93
112
  }
@@ -103,7 +122,87 @@ markdown.html = html => {
103
122
  return html
104
123
  }
105
124
 
106
- markdown.paragraph = text => {
125
+ markdown.paragraph = token => {
126
+ const text = token.text || ''
127
+ const inlineTokens = token.tokens || []
128
+
129
+ // Check if single token that shouldn't be wrapped in <p>
130
+ if (inlineTokens.length === 1) {
131
+ const t = inlineTokens[0]
132
+ if (t.type === 'link') {
133
+ let out = `<Link to="${t.href}"`
134
+ if (t.title) out += ` title="${t.title}"`
135
+ out += ` text="${t.text}"></Link>`
136
+ return out + '\n'
137
+ }
138
+ if (t.type === 'image') {
139
+ let out = `<Img src="${t.href}"`
140
+ if (t.text) out += ` alt="${t.text}"`
141
+ if (t.title) out += ` title="${t.title}"`
142
+ return out + '/>\n'
143
+ }
144
+ if (t.type === 'codespan') {
145
+ return `<Pre lines="false">${t.text}</Pre>\n`
146
+ }
147
+ // Single html token that starts with block-level tag
148
+ if (t.type === 'html') {
149
+ const raw = t.raw || t.text || ''
150
+ if (
151
+ raw.startsWith('<a ') ||
152
+ raw.startsWith('<img') ||
153
+ raw.startsWith('<A ') ||
154
+ raw.startsWith('<IMG')
155
+ ) {
156
+ return raw + '\n'
157
+ }
158
+ }
159
+ }
160
+
161
+ // Check if paragraph is a standalone block element (link with text content)
162
+ // If tokens are [html, text, html] pattern representing a complete link, don't wrap
163
+ if (inlineTokens.length === 3) {
164
+ const [open, content, close] = inlineTokens
165
+ if (
166
+ open?.type === 'html' &&
167
+ close?.type === 'html' &&
168
+ content?.type === 'text' &&
169
+ open?.raw?.startsWith('<a ') &&
170
+ close?.raw === '</a>'
171
+ ) {
172
+ return text + '\n'
173
+ }
174
+ }
175
+
176
+ // Render inline tokens
177
+ const renderInline = tokens => {
178
+ return tokens
179
+ .map(t => {
180
+ if (t.type === 'link') {
181
+ let out = `<Link to="${t.href}"`
182
+ if (t.title) out += ` title="${t.title}"`
183
+ out += ` text="${t.text}"></Link>`
184
+ return out
185
+ }
186
+ if (t.type === 'strong' || t.type === 'em') {
187
+ const inner = t.tokens ? renderInline(t.tokens) : t.text
188
+ const tag = t.type === 'strong' ? 'b' : 'em'
189
+ return `<${tag}>${inner}</${tag}>`
190
+ }
191
+ if (t.type === 'codespan') {
192
+ return `<Pre lines="false">${t.text}</Pre>`
193
+ }
194
+ if (t.type === 'image') {
195
+ let out = `<Img src="${t.href}"`
196
+ if (t.text) out += ` alt="${t.text}"`
197
+ if (t.title) out += ` title="${t.title}"`
198
+ out += '/>'
199
+ return out
200
+ }
201
+ return t.text || ''
202
+ })
203
+ .join('')
204
+ }
205
+
107
206
  if (text.startsWith('<')) {
108
207
  const tagName = text.substr(1).split(' ')[0].split('>')[0]
109
208
 
@@ -116,7 +215,8 @@ markdown.paragraph = text => {
116
215
  }
117
216
  }
118
217
 
119
- return `<p>${text}</p>\n`
218
+ const rendered = inlineTokens.length ? renderInline(inlineTokens) : text
219
+ return `<p>${rendered}</p>\n`
120
220
  }
121
221
 
122
- markdown.strong = text => `<b>${text}</b>`
222
+ markdown.strong = token => `<b>${token.text}</b>`