@docusaurus/utils 2.4.1 → 3.0.0-beta.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.
Files changed (52) hide show
  1. package/lib/constants.d.ts +1 -1
  2. package/lib/constants.d.ts.map +1 -1
  3. package/lib/contentVisibilityUtils.d.ts +29 -0
  4. package/lib/contentVisibilityUtils.d.ts.map +1 -0
  5. package/lib/contentVisibilityUtils.js +39 -0
  6. package/lib/contentVisibilityUtils.js.map +1 -0
  7. package/lib/dataFileUtils.d.ts +1 -1
  8. package/lib/dataFileUtils.d.ts.map +1 -1
  9. package/lib/globUtils.d.ts +1 -1
  10. package/lib/globUtils.d.ts.map +1 -1
  11. package/lib/globUtils.js +2 -1
  12. package/lib/globUtils.js.map +1 -1
  13. package/lib/index.d.ts +3 -2
  14. package/lib/index.d.ts.map +1 -1
  15. package/lib/index.js +9 -2
  16. package/lib/index.js.map +1 -1
  17. package/lib/jsUtils.d.ts +4 -0
  18. package/lib/jsUtils.d.ts.map +1 -1
  19. package/lib/jsUtils.js +11 -1
  20. package/lib/jsUtils.js.map +1 -1
  21. package/lib/markdownLinks.d.ts +2 -2
  22. package/lib/markdownLinks.d.ts.map +1 -1
  23. package/lib/markdownLinks.js +38 -18
  24. package/lib/markdownLinks.js.map +1 -1
  25. package/lib/markdownUtils.d.ts +27 -2
  26. package/lib/markdownUtils.d.ts.map +1 -1
  27. package/lib/markdownUtils.js +57 -2
  28. package/lib/markdownUtils.js.map +1 -1
  29. package/lib/pathUtils.d.ts +6 -2
  30. package/lib/pathUtils.d.ts.map +1 -1
  31. package/lib/pathUtils.js +7 -3
  32. package/lib/pathUtils.js.map +1 -1
  33. package/lib/slugger.d.ts +2 -2
  34. package/lib/slugger.d.ts.map +1 -1
  35. package/lib/tags.d.ts +19 -5
  36. package/lib/tags.d.ts.map +1 -1
  37. package/lib/tags.js +21 -1
  38. package/lib/tags.js.map +1 -1
  39. package/lib/urlUtils.js +2 -2
  40. package/lib/urlUtils.js.map +1 -1
  41. package/lib/webpackUtils.d.ts +2 -2
  42. package/lib/webpackUtils.d.ts.map +1 -1
  43. package/package.json +10 -10
  44. package/src/contentVisibilityUtils.ts +54 -0
  45. package/src/globUtils.ts +4 -1
  46. package/src/index.ts +5 -0
  47. package/src/jsUtils.ts +10 -0
  48. package/src/markdownLinks.ts +56 -22
  49. package/src/markdownUtils.ts +70 -1
  50. package/src/pathUtils.ts +7 -3
  51. package/src/tags.ts +31 -0
  52. package/src/urlUtils.ts +3 -3
package/src/pathUtils.ts CHANGED
@@ -96,12 +96,16 @@ export function aliasedSitePath(filePath: string, siteDir: string): string {
96
96
  * When you have a path like C:\X\Y
97
97
  * It is not safe to use directly when generating code
98
98
  * For example, this would fail due to unescaped \:
99
- * `<img src={require('${filePath}')} />`
100
- * But this would work: `<img src={require('${escapePath(filePath)}')} />`
99
+ * `<img src={require("${filePath}")} />`
100
+ * But this would work: `<img src={require("${escapePath(filePath)}")} />`
101
101
  *
102
102
  * posixPath can't be used in all cases, because forward slashes are only valid
103
103
  * Windows paths when they don't contain non-ascii characters, and posixPath
104
104
  * doesn't escape those that fail to be converted.
105
+ *
106
+ * This function escapes double quotes but not single quotes (because it uses
107
+ * `JSON.stringify`). Therefore, you must put the escaped path inside double
108
+ * quotes when generating code.
105
109
  */
106
110
  export function escapePath(str: string): string {
107
111
  const escaped = JSON.stringify(str);
@@ -114,5 +118,5 @@ export function addTrailingPathSeparator(str: string): string {
114
118
  return str.endsWith(path.sep)
115
119
  ? str
116
120
  : // If this is Windows, we need to change the forward slash to backward
117
- `${str.replace(/\/$/, '')}${path.sep}`;
121
+ `${str.replace(/[\\/]$/, '')}${path.sep}`;
118
122
  }
package/src/tags.ts CHANGED
@@ -25,6 +25,8 @@ export type TagsListItem = Tag & {
25
25
  export type TagModule = TagsListItem & {
26
26
  /** The tags list page's permalink. */
27
27
  allTagsPath: string;
28
+ /** Is this tag unlisted? (when it only contains unlisted items) */
29
+ unlisted: boolean;
28
30
  };
29
31
 
30
32
  export type FrontMatterTag = string | Tag;
@@ -128,3 +130,32 @@ export function groupTaggedItems<Item>(
128
130
 
129
131
  return result;
130
132
  }
133
+
134
+ /**
135
+ * Permits to get the "tag visibility" (hard to find a better name)
136
+ * IE, is this tag listed or unlisted
137
+ * And which items should be listed when this tag is browsed
138
+ */
139
+ export function getTagVisibility<Item>({
140
+ items,
141
+ isUnlisted,
142
+ }: {
143
+ items: Item[];
144
+ isUnlisted: (item: Item) => boolean;
145
+ }): {
146
+ unlisted: boolean;
147
+ listedItems: Item[];
148
+ } {
149
+ const allItemsUnlisted = items.every(isUnlisted);
150
+ // When a tag is full of unlisted items, we display all the items
151
+ // when tag is browsed, but we mark the tag as unlisted
152
+ if (allItemsUnlisted) {
153
+ return {unlisted: true, listedItems: items};
154
+ }
155
+ // When a tag has some listed items, the tag remains listed
156
+ // but we filter its unlisted items
157
+ return {
158
+ unlisted: false,
159
+ listedItems: items.filter((item) => !isUnlisted(item)),
160
+ };
161
+ }
package/src/urlUtils.ts CHANGED
@@ -6,7 +6,7 @@
6
6
  */
7
7
 
8
8
  import resolvePathnameUnsafe from 'resolve-pathname';
9
- import {removeSuffix} from './jsUtils';
9
+ import {addPrefix, addSuffix, removeSuffix} from './jsUtils';
10
10
 
11
11
  /**
12
12
  * Much like `path.join`, but much better. Takes an array of URL segments, and
@@ -175,13 +175,13 @@ export function resolvePathname(to: string, from?: string): string {
175
175
  }
176
176
  /** Appends a leading slash to `str`, if one doesn't exist. */
177
177
  export function addLeadingSlash(str: string): string {
178
- return str.startsWith('/') ? str : `/${str}`;
178
+ return addPrefix(str, '/');
179
179
  }
180
180
 
181
181
  // TODO deduplicate: also present in @docusaurus/utils-common
182
182
  /** Appends a trailing slash to `str`, if one doesn't exist. */
183
183
  export function addTrailingSlash(str: string): string {
184
- return str.endsWith('/') ? str : `${str}/`;
184
+ return addSuffix(str, '/');
185
185
  }
186
186
 
187
187
  /** Removes the trailing slash from `str`. */