markdown-magic 3.4.0 → 3.4.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.
package/lib/index.js CHANGED
@@ -1,4 +1,7 @@
1
1
  const path = require('path')
2
+ // @TODO remove this once we have swapped out node-fetch@2.7
3
+ const moduleAlias = require('module-alias') // "Fix" for puncode dep warning in node 22+
4
+ moduleAlias.addAlias('punycode', 'punycode/') // "Fix" for puncode dep warning in node 22+
2
5
  const { glob, globWithGit } = require('smart-glob')
3
6
  const codeTransform = require('./transforms/code')
4
7
  const fileTransform = require('./transforms/file')
@@ -1,6 +1,8 @@
1
1
  const path = require('path')
2
2
  const fs = require('fs')
3
3
  const isLocalPath = require('is-local-path')
4
+ const { findFrontmatter } = require('@davidwells/md-utils/find-frontmatter')
5
+ const { removeLeadingH1 } = require('@davidwells/md-utils/string-utils')
4
6
 
5
7
  module.exports = function FILE(api) {
6
8
  /*
@@ -30,6 +32,37 @@ module.exports = function FILE(api) {
30
32
  // trim leading and trailing spaces/line breaks in code and keeps the indentation of the first non-empty line
31
33
  fileContents = fileContents.replace(/^(?:[\t ]*(?:\r?\n|\r))+|\s+$/g, '')
32
34
 
35
+ if (options.removeLeadingH1) {
36
+ fileContents = removeLeadingH1(fileContents)
37
+ }
38
+
39
+ const isMarkdown = path.extname(options.src).toLowerCase() === '.md'
40
+ // Shift headers up or down by the specified number of levels if shiftHeaders is enabled and file is markdown
41
+ if (options.shiftHeaders && isMarkdown) {
42
+ fileContents = fileContents.replace(/^(#{1,6})\s/gm, (match, hashes) => {
43
+ const currentLevel = hashes.length;
44
+ const shiftAmount = options.shiftHeaders;
45
+ const newLevel = Math.max(1, Math.min(6, currentLevel + shiftAmount));
46
+ return '#'.repeat(newLevel) + ' ';
47
+ })
48
+ }
49
+
50
+ /* automatically trim frontmatter if file is markdown */
51
+ if (isMarkdown && options.trimFrontmatter !== false) {
52
+ const frontmatter = findFrontmatter(fileContents)
53
+ if (frontmatter && frontmatter.frontMatterRaw) {
54
+ fileContents = fileContents.replace(frontmatter.frontMatterRaw, '')
55
+ }
56
+ }
57
+
58
+ if (options.textBefore) {
59
+ fileContents = `${options.textBefore}${fileContents}`
60
+ }
61
+
62
+ if (options.textAfter) {
63
+ fileContents = `${fileContents}${options.textAfter}`
64
+ }
65
+
33
66
  return fileContents
34
67
 
35
68
  return `<!-- The below content is automatically added from ${options.src} -->
package/package.json CHANGED
@@ -1,11 +1,12 @@
1
1
  {
2
2
  "name": "markdown-magic",
3
- "version": "3.4.0",
3
+ "version": "3.4.2",
4
4
  "description": "Automatically update markdown files with content from external sources",
5
5
  "main": "lib/index.js",
6
6
  "bin": {
7
7
  "markdown": "./cli.js",
8
- "md-magic": "./cli.js"
8
+ "md-magic": "./cli.js",
9
+ "mdm": "./cli.js"
9
10
  },
10
11
  "files": [
11
12
  "README.md",
@@ -43,16 +44,18 @@
43
44
  "url": "https://github.com/DavidWells/markdown-magic"
44
45
  },
45
46
  "dependencies": {
46
- "@davidwells/md-utils": "^0.0.44",
47
+ "@davidwells/md-utils": "^0.0.46",
47
48
  "globrex": "^0.1.2",
48
49
  "gray-matter": "^4.0.3",
49
50
  "is-glob": "^4.0.3",
50
51
  "is-local-path": "^0.1.6",
51
52
  "is-valid-path": "^0.1.1",
52
53
  "micro-mdx-parser": "^1.1.0",
54
+ "module-alias": "^2.2.3",
53
55
  "mri": "^1.2.0",
54
56
  "node-fetch": "^2.7.0",
55
57
  "oparser": "^3.0.22",
58
+ "punycode": "^2.3.1",
56
59
  "smart-glob": "^1.0.2",
57
60
  "string-width": "^4.2.3",
58
61
  "sync-request": "^6.1.0"