@diplodoc/transform 4.31.4-beta1 → 4.32.2

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 (40) hide show
  1. package/dist/css/print.css.map +2 -2
  2. package/dist/css/yfm.css +6 -6
  3. package/dist/css/yfm.css.map +3 -3
  4. package/dist/css/yfm.min.css +1 -1
  5. package/dist/css/yfm.min.css.map +3 -3
  6. package/dist/js/yfm.js +2 -2
  7. package/lib/frontmatter/common.d.ts +21 -0
  8. package/lib/frontmatter/common.js +24 -0
  9. package/lib/frontmatter/common.js.map +1 -0
  10. package/lib/frontmatter/emplace.d.ts +4 -0
  11. package/lib/frontmatter/emplace.js +21 -0
  12. package/lib/frontmatter/emplace.js.map +1 -0
  13. package/lib/frontmatter/extract.d.ts +8 -0
  14. package/lib/frontmatter/extract.js +65 -0
  15. package/lib/frontmatter/extract.js.map +1 -0
  16. package/lib/frontmatter/index.d.ts +4 -0
  17. package/lib/frontmatter/index.js +23 -0
  18. package/lib/frontmatter/index.js.map +1 -0
  19. package/lib/frontmatter/transformValues.d.ts +2 -0
  20. package/lib/frontmatter/transformValues.js +20 -0
  21. package/lib/frontmatter/transformValues.js.map +1 -0
  22. package/lib/index.js +3 -1
  23. package/lib/index.js.map +1 -1
  24. package/lib/liquid/index.d.ts +9 -2
  25. package/lib/liquid/index.js +34 -4
  26. package/lib/liquid/index.js.map +1 -1
  27. package/lib/md.js +0 -1
  28. package/lib/md.js.map +1 -1
  29. package/lib/utilsFS.js.map +1 -1
  30. package/package.json +2 -2
  31. package/src/scss/_note.scss +7 -4
  32. package/src/transform/frontmatter/common.ts +27 -0
  33. package/src/transform/frontmatter/emplace.ts +24 -0
  34. package/src/transform/frontmatter/extract.ts +94 -0
  35. package/src/transform/frontmatter/index.ts +4 -0
  36. package/src/transform/frontmatter/transformValues.ts +26 -0
  37. package/src/transform/index.ts +4 -2
  38. package/src/transform/liquid/index.ts +73 -4
  39. package/src/transform/md.ts +0 -1
  40. package/src/transform/utilsFS.ts +2 -2
@@ -1,5 +1,13 @@
1
1
  import type {Dictionary} from 'lodash';
2
2
 
3
+ import {
4
+ countLineAmount,
5
+ emplaceSerializedFrontMatter,
6
+ separateAndExtractFrontMatter,
7
+ serializeFrontMatter,
8
+ transformFrontMatterValues,
9
+ } from '../frontmatter';
10
+
3
11
  import applySubstitutions from './substitutions';
4
12
  import {prepareSourceMap} from './sourceMap';
5
13
  import applyCycles from './cycles';
@@ -66,7 +74,7 @@ function repairCode(str: string, codes: string[]) {
66
74
  return replace(fence, fence, (code) => codes[Number(code)], str);
67
75
  }
68
76
 
69
- function liquid<
77
+ function liquidSnippet<
70
78
  B extends boolean = false,
71
79
  C = B extends false ? string : {output: string; sourceMap: Dictionary<string>},
72
80
  >(
@@ -141,6 +149,67 @@ function liquid<
141
149
  return output as unknown as C;
142
150
  }
143
151
 
144
- // 'export default' instead of 'export = ' because of circular dependency with './cycles.ts'.
145
- // somehow it breaks import in './cycles.ts' and imports nothing
146
- export default liquid;
152
+ type TransformSourceMapOptions = {
153
+ emplacedResultOffset: number;
154
+ emplacedSourceOffset: number;
155
+ };
156
+
157
+ function transformSourceMap(
158
+ sourceMap: Dictionary<string>,
159
+ {emplacedResultOffset, emplacedSourceOffset}: TransformSourceMapOptions,
160
+ ) {
161
+ return Object.fromEntries(
162
+ Object.entries(sourceMap).map(([lineInResult, lineInSource]) => [
163
+ (Number(lineInResult) + emplacedResultOffset).toString(),
164
+ (Number(lineInSource) + emplacedSourceOffset).toString(),
165
+ ]),
166
+ );
167
+ }
168
+
169
+ function liquidDocument<
170
+ B extends boolean = false,
171
+ C = B extends false ? string : {output: string; sourceMap: Dictionary<string>},
172
+ >(
173
+ originInput: string,
174
+ vars: Record<string, unknown>,
175
+ path?: string,
176
+ settings?: ArgvSettings & {withSourceMap?: B},
177
+ ): C {
178
+ const {frontMatter, frontMatterStrippedContent, frontMatterLineCount} =
179
+ separateAndExtractFrontMatter(originInput, path);
180
+
181
+ const transformedFrontMatter = transformFrontMatterValues(frontMatter, (v) =>
182
+ typeof v === 'string'
183
+ ? liquidSnippet(v, vars, path, {...settings, withSourceMap: false})
184
+ : v,
185
+ );
186
+ const transformedAndSerialized = serializeFrontMatter(transformedFrontMatter);
187
+
188
+ // -1 comes from the fact that the last line in serialized FM is the same as the first line in stripped content
189
+ const resultFrontMatterOffset = Math.max(0, countLineAmount(transformedAndSerialized) - 1);
190
+ const sourceFrontMatterOffset = Math.max(0, frontMatterLineCount - 1);
191
+
192
+ const liquidProcessedContent = liquidSnippet(frontMatterStrippedContent, vars, path, settings);
193
+
194
+ // typeof check for better inference; the catch is that return of liquidSnippet can be an
195
+ // object even with source maps off, see `substitutions.test.ts`
196
+ return (settings?.withSourceMap && typeof liquidProcessedContent === 'object'
197
+ ? {
198
+ output: emplaceSerializedFrontMatter(
199
+ liquidProcessedContent.output,
200
+ transformedAndSerialized,
201
+ ),
202
+ sourceMap: transformSourceMap(liquidProcessedContent.sourceMap, {
203
+ emplacedResultOffset: resultFrontMatterOffset,
204
+ emplacedSourceOffset: sourceFrontMatterOffset,
205
+ }),
206
+ }
207
+ : emplaceSerializedFrontMatter(
208
+ liquidProcessedContent as string,
209
+ transformedAndSerialized,
210
+ )) as unknown as C;
211
+ }
212
+
213
+ // both default and named exports for convenience
214
+ export {liquidDocument, liquidSnippet};
215
+ export default liquidDocument;
@@ -97,7 +97,6 @@ function initPlugins(md: MarkdownIt, options: OptionsType, pluginOptions: Markdo
97
97
  } = options;
98
98
 
99
99
  // Need for ids of headers
100
- // @ts-ignore
101
100
  md.use(attrs, {leftDelimiter, rightDelimiter});
102
101
 
103
102
  plugins.forEach((plugin) => md.use(plugin, pluginOptions));
@@ -4,7 +4,7 @@ import {readFileSync, statSync} from 'fs';
4
4
  import escapeRegExp from 'lodash/escapeRegExp';
5
5
  import {join, parse, relative, resolve, sep} from 'path';
6
6
 
7
- import liquid from './liquid';
7
+ import liquidSnippet from './liquid';
8
8
  import {StateCore} from './typings';
9
9
  import {defaultTransformLink} from './utils';
10
10
 
@@ -68,7 +68,7 @@ export function getFileTokens(path: string, state: StateCore, options: GetFileTo
68
68
  let sourceMap;
69
69
 
70
70
  if (!disableLiquid) {
71
- const liquidResult = liquid(content, builtVars, path, {
71
+ const liquidResult = liquidSnippet(content, builtVars, path, {
72
72
  withSourceMap: true,
73
73
  conditionsInCode,
74
74
  });