@ape-egg/codie 0.1.4 → 0.1.5

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.
Files changed (2) hide show
  1. package/editable.js +17 -3
  2. package/package.json +1 -1
package/editable.js CHANGED
@@ -1,15 +1,22 @@
1
1
  // Codie editable textarea layer
2
2
  import { CLASS } from './constants.js'
3
- import { highlightHTML, highlightJS, highlightCSS, highlightCSSOnly, highlightJSOnly } from './highlight.js'
3
+ import { highlightHTML, highlightJS, highlightCSS, highlightJSON, highlightJSRaw, highlightCSSOnly, highlightJSOnly } from './highlight.js'
4
4
  import { escapeHTML, formatDocument, normalizeInlineWhitespace, cleanupBooleanAttrs } from './format.js'
5
5
  import { updateNumberRows } from './numberRows.js'
6
6
  import keyboard from './keyboard.js'
7
7
 
8
- // Highlight code based on modes
8
+ // Highlight code based on modes — same dispatch as codie.js, so an edit
9
+ // re-renders through the same highlighter the initial display used
9
10
  const highlight = (code, modes) => {
10
11
  const escaped = escapeHTML(code)
11
12
 
12
- if (modes.html) {
13
+ const looksLikeJSON = /^\s*[{\[]/.test(code) && /"[^"]*"\s*:/.test(code)
14
+
15
+ if (modes.json && looksLikeJSON) {
16
+ return highlightJSON(escaped)
17
+ }
18
+
19
+ if (modes.html && !looksLikeJSON) {
13
20
  return highlightHTML(escaped, {
14
21
  preserveWhitespace: true,
15
22
  highlightJS: modes.js,
@@ -17,6 +24,13 @@ const highlight = (code, modes) => {
17
24
  })
18
25
  }
19
26
 
27
+ if (!modes.html) {
28
+ if (modes.json) return highlightJSON(escaped)
29
+ if (modes.css) return highlightCSS(escaped)
30
+ if (modes.js) return highlightJSRaw(code)
31
+ return escaped
32
+ }
33
+
20
34
  let result = escaped
21
35
  if (modes.js) result = highlightJSOnly(result)
22
36
  if (modes.css) result = highlightCSSOnly(result)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ape-egg/codie",
3
- "version": "0.1.4",
3
+ "version": "0.1.5",
4
4
  "description": "Modular code display and editing package",
5
5
  "type": "module",
6
6
  "main": "codie.js",