@alfacapital-tech/c4builder 0.5.0-rc.1 → 0.5.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.
@@ -87,10 +87,13 @@ const FENCE_LANGS = ['plantuml', 'puml', 'd2'];
87
87
  * Markdown одного исходного файла → страница сайта: fenced-диаграммы вырезаются, ссылки
88
88
  * на другие страницы источника (`target`, `#x` → `?id=x`) — на их адреса, прочие
89
89
  * относительные ссылки на существующие файлы источника регистрируются для копирования к
90
- * странице (картинки docsify грузит относительно страницы, обычные ссылки — от корня сайта).
91
- * `fromDir` — posix-каталог файла относительно `srcRoot` (ссылки в нём резолвятся от файла).
90
+ * странице (картинки и embed docsify грузит относительно страницы, обычные ссылки — от корня
91
+ * сайта). Embed `':include'` markdown-файла встраивается при сборке: сырой файл docsify
92
+ * показал бы без локального рендера диаграмм и переписывания ссылок.
93
+ * `fromDir` — posix-каталог файла относительно `srcRoot` (ссылки в нём резолвятся от файла),
94
+ * `pageDir` — каталог, от которого считаются пути файлов страницы (у встроенного — каталог страницы).
92
95
  */
93
- export const renderMarkdown = (pagePath, base, content, { srcRoot, fromDir = '', target = () => undefined, source, options }) => {
96
+ export const renderMarkdown = (pagePath, base, content, { srcRoot, fromDir = '', pageDir = fromDir, included = new Set(), target = () => undefined, source, options }) => {
94
97
  const { markdown, diagrams } = createFenceExtractor(FENCE_LANGS).extract(content, base, source);
95
98
  const files = [];
96
99
  const rewrite = (t) => t.replace(LINK_RE, (whole, bang, text, href, title = '') => {
@@ -114,10 +117,28 @@ export const renderMarkdown = (pagePath, base, content, { srcRoot, fromDir = '',
114
117
  const abs = path.join(srcRoot, rel);
115
118
  if (!fs.existsSync(abs) || fs.statSync(abs).isDirectory())
116
119
  return whole;
117
- files.push({ from: rel, to: decoded });
118
- return bang
119
- ? `![${inner}](${href}${title})`
120
- : `[${inner}](${encodeURIPath(path.posix.join(...pagePath, decoded))}${ignoreTitle(title)})`;
120
+ const embed = !bang && /:include\b/.test(title);
121
+ if (embed && /\.md$/i.test(rel) && !included.has(rel)) {
122
+ const dir = path.posix.dirname(rel);
123
+ const sub = renderMarkdown(pagePath, base, fs.readFileSync(abs, 'utf8'), {
124
+ srcRoot,
125
+ fromDir: dir === '.' ? '' : dir,
126
+ pageDir,
127
+ included: new Set([...included, rel]),
128
+ target,
129
+ source: relSource(srcRoot, rel),
130
+ options
131
+ });
132
+ diagrams.push(...sub.diagrams);
133
+ files.push(...sub.files);
134
+ return sub.markdown;
135
+ }
136
+ const sameDir = fromDir === pageDir;
137
+ const to = sameDir ? decoded : path.posix.relative(pageDir, rel);
138
+ files.push({ from: rel, to });
139
+ return bang || embed
140
+ ? `${bang}[${inner}](${sameDir ? href : encodeURIPath(to)}${title})`
141
+ : `[${inner}](${encodeURIPath(path.posix.join(...pagePath, to))}${ignoreTitle(title)})`;
121
142
  });
122
143
  return { path: pagePath, markdown: mapOutsideFences(markdown, rewrite).trim(), diagrams, files };
123
144
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alfacapital-tech/c4builder",
3
- "version": "0.5.0-rc.1",
3
+ "version": "0.5.1",
4
4
  "description": "A CLI tool designed to compile a folder structure of markdowns and plant uml files into a site, single file markdown or a collection of markdowns with links",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",