sommark 4.5.3 → 5.0.1

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 (41) hide show
  1. package/README.md +315 -179
  2. package/cli/cli.mjs +1 -1
  3. package/cli/commands/color.js +36 -14
  4. package/cli/commands/help.js +3 -0
  5. package/cli/commands/init.js +1 -3
  6. package/cli/constants.js +5 -2
  7. package/constants/html_props.js +0 -5
  8. package/core/errors.js +5 -4
  9. package/core/evaluator.js +1 -2
  10. package/core/formats.js +7 -1
  11. package/core/helpers/config-loader.js +2 -4
  12. package/core/helpers/lib.js +1 -1
  13. package/core/labels.js +2 -15
  14. package/core/lexer.js +197 -313
  15. package/core/modules.js +13 -13
  16. package/core/parser.js +226 -535
  17. package/core/tokenTypes.js +6 -15
  18. package/core/transpiler.js +129 -110
  19. package/core/validator.js +6 -26
  20. package/dist/sommark.browser.js +1781 -2172
  21. package/dist/sommark.browser.lite.js +1779 -2169
  22. package/dist/sommark.lexer.js +392 -544
  23. package/dist/sommark.parser.js +604 -1200
  24. package/formatter/mark.js +34 -0
  25. package/formatter/tag.js +7 -33
  26. package/helpers/utils.js +15 -16
  27. package/index.js +9 -1
  28. package/index.shared.js +26 -16
  29. package/mappers/languages/csv.js +62 -0
  30. package/mappers/languages/html.js +12 -66
  31. package/mappers/languages/json.js +74 -156
  32. package/mappers/languages/jsonc.js +21 -63
  33. package/mappers/languages/markdown.js +159 -276
  34. package/mappers/languages/mdx.js +7 -62
  35. package/mappers/languages/text.js +2 -19
  36. package/mappers/languages/toml.js +231 -0
  37. package/mappers/languages/xml.js +25 -25
  38. package/mappers/languages/yaml.js +323 -0
  39. package/mappers/mapper.js +1 -22
  40. package/mappers/shared/index.js +3 -16
  41. package/package.json +5 -2
@@ -1,31 +1,53 @@
1
1
  import { formatMessage } from "../../core/errors.js";
2
+ import fs from "node:fs/promises";
3
+ import path from "node:path";
4
+ import os from "node:os";
5
+
6
+ const SOMMARK_CONFIG_DIR = path.join(os.homedir(), ".sommark");
7
+ const SOMMARK_CONFIG_FILE = path.join(SOMMARK_CONFIG_DIR, "config.json");
8
+
9
+ /**
10
+ * Reads the user-level SomMark config from ~/.sommark/config.json.
11
+ * @returns {Promise<Object>}
12
+ */
13
+ async function readUserConfig() {
14
+ try {
15
+ const raw = await fs.readFile(SOMMARK_CONFIG_FILE, "utf-8");
16
+ return JSON.parse(raw);
17
+ } catch {
18
+ return {};
19
+ }
20
+ }
21
+
22
+ /**
23
+ * Writes a key/value pair to ~/.sommark/config.json.
24
+ */
25
+ async function writeUserConfig(data) {
26
+ await fs.mkdir(SOMMARK_CONFIG_DIR, { recursive: true });
27
+ const existing = await readUserConfig();
28
+ await fs.writeFile(SOMMARK_CONFIG_FILE, JSON.stringify({ ...existing, ...data }, null, 2));
29
+ }
2
30
 
3
31
  /**
4
- * Checks if colors are turned on in your settings.
32
+ * Checks if colors are enabled reads from user config or SOMMARK_COLOR env var.
5
33
  * @returns {Promise<boolean>}
6
34
  */
7
35
  export async function isColorEnabled() {
8
- return process.env.SOMMARK_COLOR === "true";
36
+ if (process.env.SOMMARK_COLOR === "true") return true;
37
+ if (process.env.SOMMARK_COLOR === "false") return false;
38
+ const config = await readUserConfig();
39
+ return config.color === true;
9
40
  }
10
41
 
11
42
  /**
12
- * Shows the user how to turn colors on or off.
43
+ * Instructs the user how to enable or disable color output via the environment variable.
13
44
  * @param {string} action - 'on' or 'off'.
14
45
  */
15
46
  export function runColor(action) {
16
47
  if (action === "on") {
17
- console.log(formatMessage([
18
- `{line}<$yellow:SomMark uses Environment Variables for colors:$>{line}`,
19
- `<$blue:Set this in your current shell:$>`,
20
- ` <$cyan:export SOMMARK_COLOR=true$>{line}`,
21
- `<$blue:Add it to your .bashrc or .zshrc for permanent effect.$>{line}`
22
- ].join("")));
48
+ console.log(formatMessage(`Set <$cyan:SOMMARK_COLOR=true$> in your shell environment to enable colors.`));
23
49
  } else if (action === "off") {
24
- console.log(formatMessage([
25
- `{line}<$yellow:To disable colors, run:$>{line}`,
26
- ` <$cyan:export SOMMARK_COLOR=false$>{line}`,
27
- `<$blue:(Or remove the SOMMARK_COLOR variable from your shell config)$>{line}`
28
- ].join("")));
50
+ console.log(formatMessage(`Set <$cyan:SOMMARK_COLOR=false$> in your shell environment to disable colors.`));
29
51
  } else {
30
52
  console.log(formatMessage(`Usage: <$blue:sommark color on|off$>`));
31
53
  }
@@ -36,6 +36,9 @@ export function getHelp(unknown_option = true) {
36
36
  "{N} <$green:--json$> <$cyan: Transpile to JSON$>",
37
37
  "{N} <$green:--jsonc$> <$cyan: Transpile to JSON with Comments (JSONC)$>",
38
38
  "{N} <$green:--text$> <$cyan: Transpile to plain text$>",
39
+ "{N} <$green:--csv$> <$cyan: Transpile to CSV$>",
40
+ "{N} <$green:--toml$> <$cyan: Transpile to TOML$>",
41
+ "{N} <$green:--yaml$> <$cyan: Transpile to YAML$>",
39
42
  "{N} <$green:--lex$> <$cyan: Print lexer tokens to console$>",
40
43
  "{N} <$green:--parse$> <$cyan: Print parser AST to console$>",
41
44
 
@@ -21,14 +21,12 @@ export async function runInit() {
21
21
  export default {
22
22
  format: "html", // Target output format (html, markdown, mdx, json, xml, jsonc, text)
23
23
  removeComments: true, // Strip SomMark comments from the final output
24
- generateRuntimeOutput: false, // Generate only VM script bundles, ignoring markup layouts
25
- hideRuntimeOutput: false, // Strip all <script> tags from final compiled HTML outputs
26
24
  customProps: [], // Whitelisted HTML attributes
27
25
  placeholders: {}, // Global p{key} placeholders for content injection
28
26
  importAliases: { // Custom path aliases for modules (e.g. { "@": "./src/components" })
29
27
  "@": "./"
30
28
  },
31
- fallbackTarget: "style", // Where unrecognized attributes go: "style", "class", or false to disable
29
+ fallbackTarget: true, // Enable style fallback for unrecognized attributes (true or false)
32
30
  outputValidator: null, // Custom callback function: async (transpiledOutput) => { ... }
33
31
  baseDir: null, // Base directory for resolving relative module imports
34
32
  showSpinner: true, // Display a dynamic spinner in the terminal during transpilation
package/cli/constants.js CHANGED
@@ -4,7 +4,7 @@
4
4
  */
5
5
 
6
6
  /** @type {Array<string>} List of recognized CLI flags and commands. */
7
- export const options = ["-v", "--version", "-h", "--help", "--html", "--markdown", "--mdx", "--json", "--jsonc", "--text", "--xml", "--print", "-p", "--lex", "--parse", "list", "bundle"];
7
+ export const options = ["-v", "--version", "-h", "--help", "--html", "--markdown", "--mdx", "--json", "--jsonc", "--text", "--xml", "--csv", "--toml", "--yaml", "--print", "-p", "--lex", "--parse", "list", "bundle"];
8
8
 
9
9
  /** @type {Object<string, string>} Map of output formats to their respective file extensions. */
10
10
  export const extensions = {
@@ -14,5 +14,8 @@ export const extensions = {
14
14
  mdx: "mdx",
15
15
  json: "json",
16
16
  jsonc: "jsonc",
17
- xml: "xml"
17
+ xml: "xml",
18
+ csv: "csv",
19
+ toml: "toml",
20
+ yaml: "yaml"
18
21
  };
@@ -150,10 +150,6 @@ export const HTML_PROPS = new Set([
150
150
  "optimum",
151
151
 
152
152
  // Deprecated (still valid)
153
- "align",
154
- "bgcolor",
155
- "border",
156
- "color",
157
153
  "coords",
158
154
  "shape",
159
155
  "summary",
@@ -161,6 +157,5 @@ export const HTML_PROPS = new Set([
161
157
  // Experimental
162
158
  "elementtiming",
163
159
  "colorspace",
164
- "alpha",
165
160
  "csp",
166
161
  ]);
package/core/errors.js CHANGED
@@ -20,17 +20,18 @@ import colorize from "../helpers/colorize.js";
20
20
  * @returns {string} - The final formatted and colored string.
21
21
  */
22
22
  function formatMessage(text) {
23
- const horizontal_rule = "\n----------------------------------------------------------------------------------------------\n";
23
+ const horizontal_rule = "\n" + colorize("blue", "-".repeat(90)) + "\n";
24
24
  const pattern = /<\$([^:]+):([\s\S]*?)\$>/g;
25
25
 
26
26
  if (Array.isArray(text)) {
27
27
  text = text.join("");
28
28
  }
29
29
 
30
+ // Apply {line} before color tags so the rule is never nested inside a color wrapper.
31
+ text = text.replaceAll("{line}", horizontal_rule);
30
32
  text = text.replace(pattern, (match, color, content) => {
31
33
  return colorize(color, content.trim());
32
34
  });
33
- text = text.replaceAll("{line}", horizontal_rule);
34
35
  text = text.replaceAll("{N}", "\n");
35
36
 
36
37
  text = text
@@ -68,11 +69,11 @@ function formatErrorWithContext(src, range, filename, message, typeName) {
68
69
  : `from line <$yellow:${range.start.line + 1}$>, column <$yellow:${range.start.character}$> to line <$yellow:${range.end.line + 1}$>, column <$yellow:${range.end.character}$>`;
69
70
 
70
71
  const formattedMessage = [
71
- `<$blue:{line}$><$red:Here where error occurred${sourceLabel}:$>{N}${lineContent}{N}${pointerPadding}<$yellow:^$>{N}{N}`,
72
+ `{line}<$red:Here where error occurred${sourceLabel}:$>{N}${lineContent}{N}${pointerPadding}<$yellow:^$>{N}`,
72
73
  `<$red:${typeName} Error:$> `,
73
74
  ...(Array.isArray(message) ? message : [message]),
74
75
  `{N}at line <$yellow:${range.start.line + 1}$>, ${rangeInfo}{N}`,
75
- "<$blue:{line}$>"
76
+ `{line}`
76
77
  ];
77
78
 
78
79
  return formattedMessage;
package/core/evaluator.js CHANGED
@@ -644,10 +644,9 @@ class EvaluatorState {
644
644
  const tag = SomMark.__dynamicTags.get(${JSON.stringify(id)});
645
645
  if (!tag) throw new Error("Tag not found inside VM: " + ${JSON.stringify(id)});
646
646
  const res = tag.render({
647
- args: payload.args,
647
+ props: payload.props,
648
648
  content: payload.content,
649
649
  textContent: payload.textContent,
650
- nodeType: payload.nodeType,
651
650
  isSelfClosing: payload.isSelfClosing
652
651
  });
653
652
  return res;
package/core/formats.js CHANGED
@@ -8,6 +8,9 @@ export const mdxFormat = "mdx";
8
8
  export const jsonFormat = "json";
9
9
  export const jsoncFormat = "jsonc";
10
10
  export const xmlFormat = "xml";
11
+ export const csvFormat = "csv";
12
+ export const tomlFormat = "toml";
13
+ export const yamlFormat = "yaml";
11
14
 
12
15
  const formats = {
13
16
  textFormat,
@@ -16,7 +19,10 @@ const formats = {
16
19
  mdxFormat,
17
20
  jsonFormat,
18
21
  jsoncFormat,
19
- xmlFormat
22
+ xmlFormat,
23
+ csvFormat,
24
+ tomlFormat,
25
+ yamlFormat
20
26
  };
21
27
 
22
28
  export default formats;
@@ -83,12 +83,10 @@ export async function findAndLoadConfig(targetPath) {
83
83
  placeholders: {},
84
84
  customProps: [],
85
85
  importAliases: {},
86
- fallbackTarget: "style",
86
+ fallbackTarget: true,
87
87
  outputValidator: null,
88
88
  baseDir: null,
89
- showSpinner: false,
90
- generateRuntimeOutput: false,
91
- hideRuntimeOutput: false,
89
+ showSpinner: true,
92
90
  security: {},
93
91
  };
94
92
 
@@ -18,7 +18,7 @@ export function registerHostSettings(settings) {
18
18
  hostSettings = settings || {};
19
19
  }
20
20
 
21
- const version = "4.5.3";
21
+ const version = "5.0.1";
22
22
 
23
23
  const SomMark = {
24
24
  version,
package/core/labels.js CHANGED
@@ -4,8 +4,6 @@
4
4
  */
5
5
  export const BLOCK = "Block",
6
6
  TEXT = "Text",
7
- INLINE = "Inline",
8
- ATBLOCK = "AtBlock",
9
7
  COMMENT = "Comment",
10
8
  COMMENT_BLOCK = "CommentBlock",
11
9
  IMPORT = "Import",
@@ -18,13 +16,8 @@ export const BLOCK = "Block",
18
16
  /**
19
17
  * Names for symbols used to separate parts of the code (like commas and colons).
20
18
  */
21
- export const SEMICOLON = "Semicolon",
22
- BLOCKCOMMA = "Block-comma",
23
- ATBLOCKCOMMA = "Atblock-comma",
24
- INLINECOMMA = "Inline-comma",
25
- BLOCKCOLON = "Block-colon",
26
- ATBLOCKCOLON = "Atblock-colon",
27
- INLINECOLON = "Inline-colon";
19
+ export const BLOCKCOMMA = "Block-comma",
20
+ BLOCKCOLON = "Block-colon";
28
21
 
29
22
  /**
30
23
  * These names are used in error messages to tell you exactly which part
@@ -34,12 +27,6 @@ export const block_id = "Block Identifier",
34
27
  block_value = "Block Value",
35
28
  block_key = "Block Key",
36
29
  block_end = "Block end",
37
- inline_id = "Inline Identifier",
38
- inline_text = "Inline Text",
39
- at_id = "At Identifier",
40
- at_value = "At Value",
41
- atblock_key = "AtBlock Key",
42
- at_end = "Atblock End",
43
30
  /** Reserved keyword for closing blocks */
44
31
  end_keyword = "end",
45
32
  slot_keyword = "slot",