@abraca/convert 2.11.0 → 2.13.0

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.
@@ -89,6 +89,12 @@ function parseFrontmatter(markdown) {
89
89
  if (subtitle) meta.subtitle = subtitle;
90
90
  const url = getStr(["url"]);
91
91
  if (url) meta.url = url;
92
+ const language = getStr(["language"]);
93
+ if (language) meta.language = language;
94
+ const fileExtension = getStr(["fileExtension"]);
95
+ if (fileExtension) meta.fileExtension = fileExtension;
96
+ const codeTheme = getStr(["codeTheme"]);
97
+ if (codeTheme) meta.codeTheme = codeTheme;
92
98
  const ratingRaw = getStr(["rating"]);
93
99
  if (ratingRaw !== void 0) {
94
100
  const n = Number(ratingRaw);
@@ -1366,6 +1372,9 @@ function generateFrontmatter(label, meta, type) {
1366
1372
  4: "urgent"
1367
1373
  }[meta.priority] ?? meta.priority}`);
1368
1374
  if (meta.checked !== void 0) lines.push(`checked: ${meta.checked}`);
1375
+ if (meta.language) lines.push(`language: ${yamlScalar(meta.language)}`);
1376
+ if (meta.fileExtension) lines.push(`fileExtension: ${yamlScalar(meta.fileExtension)}`);
1377
+ if (meta.codeTheme) lines.push(`codeTheme: ${yamlScalar(meta.codeTheme)}`);
1369
1378
  if (meta.dateStart) lines.push(`dateStart: "${escapeYaml(meta.dateStart)}"`);
1370
1379
  if (meta.dateEnd) lines.push(`dateEnd: "${escapeYaml(meta.dateEnd)}"`);
1371
1380
  if (meta.subtitle) lines.push(`subtitle: "${escapeYaml(meta.subtitle)}"`);
@@ -1879,6 +1888,174 @@ function appendHtmlToFragment(fragment, html) {
1879
1888
  });
1880
1889
  }
1881
1890
 
1891
+ //#endregion
1892
+ //#region packages/convert/src/code.ts
1893
+ /** Y.Text key holding a code document's content. */
1894
+ const CODE_TEXT_KEY = "code";
1895
+ /** Read a code document's raw text. Empty string when absent. */
1896
+ function readCodeText(doc) {
1897
+ return doc.getText(CODE_TEXT_KEY).toString();
1898
+ }
1899
+ /**
1900
+ * Replace a code document's full text in a single transaction. `origin`
1901
+ * is forwarded to `transact` so callers (e.g. fs-sync) can tag the write
1902
+ * and skip their own observers.
1903
+ */
1904
+ function writeCodeText(doc, content, origin) {
1905
+ const ytext = doc.getText(CODE_TEXT_KEY);
1906
+ doc.transact(() => {
1907
+ if (ytext.length > 0) ytext.delete(0, ytext.length);
1908
+ if (content.length > 0) ytext.insert(0, content);
1909
+ }, origin);
1910
+ }
1911
+ const LANGUAGE_BY_EXT = {
1912
+ js: "javascript",
1913
+ mjs: "javascript",
1914
+ cjs: "javascript",
1915
+ jsx: "jsx",
1916
+ ts: "typescript",
1917
+ mts: "typescript",
1918
+ cts: "typescript",
1919
+ tsx: "tsx",
1920
+ html: "html",
1921
+ htm: "html",
1922
+ css: "css",
1923
+ scss: "scss",
1924
+ less: "less",
1925
+ vue: "vue",
1926
+ svelte: "svelte",
1927
+ json: "json",
1928
+ jsonc: "json",
1929
+ json5: "json",
1930
+ yaml: "yaml",
1931
+ yml: "yaml",
1932
+ toml: "toml",
1933
+ xml: "xml",
1934
+ ini: "properties",
1935
+ env: "properties",
1936
+ rs: "rust",
1937
+ go: "go",
1938
+ c: "c",
1939
+ h: "c",
1940
+ cpp: "cpp",
1941
+ cc: "cpp",
1942
+ cxx: "cpp",
1943
+ hpp: "cpp",
1944
+ cs: "csharp",
1945
+ java: "java",
1946
+ kt: "kotlin",
1947
+ kts: "kotlin",
1948
+ swift: "swift",
1949
+ m: "objective-c",
1950
+ mm: "objective-c",
1951
+ py: "python",
1952
+ pyi: "python",
1953
+ rb: "ruby",
1954
+ php: "php",
1955
+ pl: "perl",
1956
+ lua: "lua",
1957
+ r: "r",
1958
+ sh: "shell",
1959
+ bash: "shell",
1960
+ zsh: "shell",
1961
+ fish: "shell",
1962
+ sql: "sql",
1963
+ graphql: "graphql",
1964
+ gql: "graphql",
1965
+ dockerfile: "dockerfile",
1966
+ md: "markdown",
1967
+ markdown: "markdown"
1968
+ };
1969
+ /** Preferred on-disk extension per language (reverse of LANGUAGE_BY_EXT). */
1970
+ const EXT_BY_LANGUAGE = {
1971
+ javascript: "js",
1972
+ jsx: "jsx",
1973
+ typescript: "ts",
1974
+ tsx: "tsx",
1975
+ html: "html",
1976
+ css: "css",
1977
+ scss: "scss",
1978
+ less: "less",
1979
+ vue: "vue",
1980
+ svelte: "svelte",
1981
+ json: "json",
1982
+ yaml: "yaml",
1983
+ toml: "toml",
1984
+ xml: "xml",
1985
+ properties: "ini",
1986
+ rust: "rs",
1987
+ go: "go",
1988
+ c: "c",
1989
+ cpp: "cpp",
1990
+ csharp: "cs",
1991
+ java: "java",
1992
+ kotlin: "kt",
1993
+ swift: "swift",
1994
+ "objective-c": "m",
1995
+ python: "py",
1996
+ ruby: "rb",
1997
+ php: "php",
1998
+ perl: "pl",
1999
+ lua: "lua",
2000
+ r: "r",
2001
+ shell: "sh",
2002
+ sql: "sql",
2003
+ graphql: "graphql",
2004
+ dockerfile: "dockerfile",
2005
+ markdown: "md",
2006
+ plain: "txt"
2007
+ };
2008
+ /** Normalize an extension: strip leading dot(s), lowercase. */
2009
+ function normalizeExtension(ext) {
2010
+ return ext.replace(/^\.+/, "").toLowerCase();
2011
+ }
2012
+ /**
2013
+ * Whether a file extension should import as a `code` page type. Note
2014
+ * `md`/`markdown` are intentionally NOT code extensions here — fs-sync
2015
+ * treats `.md` as the prose/envelope format, so it's excluded from the
2016
+ * code-import gate even though it has a language mapping for in-editor use.
2017
+ */
2018
+ function isCodeExtension(ext) {
2019
+ const e = normalizeExtension(ext);
2020
+ if (e === "md" || e === "markdown") return false;
2021
+ return e in LANGUAGE_BY_EXT;
2022
+ }
2023
+ /** CodeMirror language id for an extension, or undefined when unknown. */
2024
+ function extToLanguage(ext) {
2025
+ return LANGUAGE_BY_EXT[normalizeExtension(ext)];
2026
+ }
2027
+ /** Preferred on-disk extension (no dot) for a language, or undefined. */
2028
+ function languageToExtension(language) {
2029
+ return EXT_BY_LANGUAGE[language.toLowerCase()];
2030
+ }
2031
+ /**
2032
+ * Derive `{ language, fileExtension }` from a filename for code import.
2033
+ * Returns undefined when the extension is not a recognized code extension
2034
+ * (so callers fall back to the prose/doc path).
2035
+ */
2036
+ function inferCodeMetaFromFilename(filename) {
2037
+ const dot = filename.lastIndexOf(".");
2038
+ if (dot < 0) return void 0;
2039
+ const ext = normalizeExtension(filename.slice(dot + 1));
2040
+ if (!isCodeExtension(ext)) return void 0;
2041
+ const language = LANGUAGE_BY_EXT[ext];
2042
+ if (!language) return void 0;
2043
+ return {
2044
+ language,
2045
+ fileExtension: ext
2046
+ };
2047
+ }
2048
+ /**
2049
+ * Choose the on-disk extension for a code doc on export. Prefers an
2050
+ * explicit `fileExtension`, then the language's primary extension, then
2051
+ * `txt`.
2052
+ */
2053
+ function codeFileExtension(meta) {
2054
+ if (meta?.fileExtension) return normalizeExtension(meta.fileExtension);
2055
+ if (meta?.language) return languageToExtension(meta.language) ?? "txt";
2056
+ return "txt";
2057
+ }
2058
+
1882
2059
  //#endregion
1883
2060
  //#region packages/convert/src/diff.ts
1884
2061
  function attrsToJson(el) {
@@ -3133,6 +3310,18 @@ function buildRelativePath(docId, treeData, manifest) {
3133
3310
  return `${parentPath}${parentPath ? "/" : ""}${resolvedFilename}.md`;
3134
3311
  }
3135
3312
  /**
3313
+ * Companion code-file path for a `code` doc, derived from its `.md`
3314
+ * envelope path by swapping the extension. e.g.
3315
+ * `src/main.md` + `rs` -> `src/main.rs`. A code doc never has children,
3316
+ * so the `_index.md` form is not expected here, but is handled defensively.
3317
+ */
3318
+ function codeCompanionPath(mdRelativePath, fileExtension) {
3319
+ const ext = fileExtension.replace(/^\.+/, "").toLowerCase() || "txt";
3320
+ if (mdRelativePath.endsWith("/_index.md")) return `${mdRelativePath.slice(0, -10)}/_index.${ext}`;
3321
+ if (mdRelativePath.endsWith(".md")) return `${mdRelativePath.slice(0, -3)}.${ext}`;
3322
+ return `${mdRelativePath}.${ext}`;
3323
+ }
3324
+ /**
3136
3325
  * Get the directory portion of a relative path for a doc.
3137
3326
  * For _index.md docs: returns the directory containing _index.md
3138
3327
  * For leaf docs: returns the parent directory
@@ -3188,5 +3377,5 @@ function nextOrder(treeData, parentId) {
3188
3377
  }
3189
3378
 
3190
3379
  //#endregion
3191
- export { MARK_SPECS, MARK_SPEC_BY_DELIM, MARK_SPEC_BY_NAME, NODE_SPECS, NODE_SPEC_BY_NAME, UNIVERSAL_META_KEYS, UNIVERSAL_META_KEY_NAMES, appendHtmlToFragment, buildAliasMap, buildRelativePath, buildReverseLookup, conflictsDir, createEmptyManifest, diffJson, diffYjs, filenameToLabel, fsFilenameToLabel, getDocDir, getFsAdapter, getTreeData, hasChildren, jsonToYFragment, labelToFilename, loadManifest, lookupByDocId, lookupByHash, lookupByPath, manifestDir, mdcTagOf, nextOrder, orphansDir, parseFrontmatter, populateYDocFromHtml, populateYDocFromMarkdown, removeEntry, resolveParentFromPath, saveManifest, setEntry, setFsAdapter, simpleHash, stringifyJsonNode, trashDir, yfragmentToJson, yjsToHtml, yjsToMarkdown, yjsToPlainText };
3380
+ export { CODE_TEXT_KEY, MARK_SPECS, MARK_SPEC_BY_DELIM, MARK_SPEC_BY_NAME, NODE_SPECS, NODE_SPEC_BY_NAME, UNIVERSAL_META_KEYS, UNIVERSAL_META_KEY_NAMES, appendHtmlToFragment, buildAliasMap, buildRelativePath, buildReverseLookup, codeCompanionPath, codeFileExtension, conflictsDir, createEmptyManifest, diffJson, diffYjs, extToLanguage, filenameToLabel, fsFilenameToLabel, getDocDir, getFsAdapter, getTreeData, hasChildren, inferCodeMetaFromFilename, isCodeExtension, jsonToYFragment, labelToFilename, languageToExtension, loadManifest, lookupByDocId, lookupByHash, lookupByPath, manifestDir, mdcTagOf, nextOrder, normalizeExtension, orphansDir, parseFrontmatter, populateYDocFromHtml, populateYDocFromMarkdown, readCodeText, removeEntry, resolveParentFromPath, saveManifest, setEntry, setFsAdapter, simpleHash, stringifyJsonNode, trashDir, writeCodeText, yfragmentToJson, yjsToHtml, yjsToMarkdown, yjsToPlainText };
3192
3381
  //# sourceMappingURL=abracadabra-convert.esm.js.map