@docusaurus/plugin-content-blog 0.0.0-5635 → 0.0.0-5636

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 (3) hide show
  1. package/lib/feed.js +27 -3
  2. package/package.json +10 -9
  3. package/src/feed.ts +34 -3
package/lib/feed.js CHANGED
@@ -12,6 +12,7 @@ const path_1 = tslib_1.__importDefault(require("path"));
12
12
  const fs_extra_1 = tslib_1.__importDefault(require("fs-extra"));
13
13
  const logger_1 = tslib_1.__importDefault(require("@docusaurus/logger"));
14
14
  const feed_1 = require("feed");
15
+ const srcset = tslib_1.__importStar(require("srcset"));
15
16
  const utils_1 = require("@docusaurus/utils");
16
17
  const utils_common_1 = require("@docusaurus/utils-common");
17
18
  const cheerio_1 = require("cheerio");
@@ -55,11 +56,34 @@ async function defaultCreateFeedItems({ blogPosts, siteConfig, outDir, }) {
55
56
  const { metadata: { title: metadataTitle, permalink, date, description, authors, tags, }, } = post;
56
57
  const content = await (0, utils_1.readOutputHTMLFile)(permalink.replace(siteConfig.baseUrl, ''), outDir, siteConfig.trailingSlash);
57
58
  const $ = (0, cheerio_1.load)(content);
58
- const link = (0, utils_1.normalizeUrl)([siteUrl, permalink]);
59
+ const blogPostAbsoluteUrl = (0, utils_1.normalizeUrl)([siteUrl, permalink]);
60
+ const toAbsoluteUrl = (src) => String(new URL(src, blogPostAbsoluteUrl));
61
+ // Make links and image urls absolute
62
+ // See https://github.com/facebook/docusaurus/issues/9136
63
+ $(`div#${utils_common_1.blogPostContainerID} a, div#${utils_common_1.blogPostContainerID} img`).each((_, elm) => {
64
+ if (elm.tagName === 'a') {
65
+ const { href } = elm.attribs;
66
+ if (href) {
67
+ elm.attribs.href = toAbsoluteUrl(href);
68
+ }
69
+ }
70
+ else if (elm.tagName === 'img') {
71
+ const { src, srcset: srcsetAttr } = elm.attribs;
72
+ if (src) {
73
+ elm.attribs.src = toAbsoluteUrl(src);
74
+ }
75
+ if (srcsetAttr) {
76
+ elm.attribs.srcset = srcset.stringify(srcset.parse(srcsetAttr).map((props) => ({
77
+ ...props,
78
+ url: toAbsoluteUrl(props.url),
79
+ })));
80
+ }
81
+ }
82
+ });
59
83
  const feedItem = {
60
84
  title: metadataTitle,
61
- id: link,
62
- link,
85
+ id: blogPostAbsoluteUrl,
86
+ link: blogPostAbsoluteUrl,
63
87
  date,
64
88
  description,
65
89
  // Atom feed demands the "term", while other feeds use "name"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@docusaurus/plugin-content-blog",
3
- "version": "0.0.0-5635",
3
+ "version": "0.0.0-5636",
4
4
  "description": "Blog plugin for Docusaurus.",
5
5
  "main": "lib/index.js",
6
6
  "types": "src/plugin-content-blog.d.ts",
@@ -19,18 +19,19 @@
19
19
  },
20
20
  "license": "MIT",
21
21
  "dependencies": {
22
- "@docusaurus/core": "0.0.0-5635",
23
- "@docusaurus/logger": "0.0.0-5635",
24
- "@docusaurus/mdx-loader": "0.0.0-5635",
25
- "@docusaurus/types": "0.0.0-5635",
26
- "@docusaurus/utils": "0.0.0-5635",
27
- "@docusaurus/utils-common": "0.0.0-5635",
28
- "@docusaurus/utils-validation": "0.0.0-5635",
22
+ "@docusaurus/core": "0.0.0-5636",
23
+ "@docusaurus/logger": "0.0.0-5636",
24
+ "@docusaurus/mdx-loader": "0.0.0-5636",
25
+ "@docusaurus/types": "0.0.0-5636",
26
+ "@docusaurus/utils": "0.0.0-5636",
27
+ "@docusaurus/utils-common": "0.0.0-5636",
28
+ "@docusaurus/utils-validation": "0.0.0-5636",
29
29
  "cheerio": "^1.0.0-rc.12",
30
30
  "feed": "^4.2.2",
31
31
  "fs-extra": "^11.1.1",
32
32
  "lodash": "^4.17.21",
33
33
  "reading-time": "^1.5.0",
34
+ "srcset": "^4.0.0",
34
35
  "tslib": "^2.6.0",
35
36
  "unist-util-visit": "^2.0.3",
36
37
  "utility-types": "^3.10.0",
@@ -43,5 +44,5 @@
43
44
  "engines": {
44
45
  "node": ">=16.14"
45
46
  },
46
- "gitHead": "b9b37df155d491fa570c242f7c151c7b5e4fb7e6"
47
+ "gitHead": "92ff1f7e897e6c77823f5c09dc9b89761c3cd83c"
47
48
  }
package/src/feed.ts CHANGED
@@ -9,6 +9,7 @@ import path from 'path';
9
9
  import fs from 'fs-extra';
10
10
  import logger from '@docusaurus/logger';
11
11
  import {Feed, type Author as FeedAuthor} from 'feed';
12
+ import * as srcset from 'srcset';
12
13
  import {normalizeUrl, readOutputHTMLFile} from '@docusaurus/utils';
13
14
  import {blogPostContainerID} from '@docusaurus/utils-common';
14
15
  import {load as cheerioLoad} from 'cheerio';
@@ -110,11 +111,41 @@ async function defaultCreateFeedItems({
110
111
  );
111
112
  const $ = cheerioLoad(content);
112
113
 
113
- const link = normalizeUrl([siteUrl, permalink]);
114
+ const blogPostAbsoluteUrl = normalizeUrl([siteUrl, permalink]);
115
+
116
+ const toAbsoluteUrl = (src: string) =>
117
+ String(new URL(src, blogPostAbsoluteUrl));
118
+
119
+ // Make links and image urls absolute
120
+ // See https://github.com/facebook/docusaurus/issues/9136
121
+ $(`div#${blogPostContainerID} a, div#${blogPostContainerID} img`).each(
122
+ (_, elm) => {
123
+ if (elm.tagName === 'a') {
124
+ const {href} = elm.attribs;
125
+ if (href) {
126
+ elm.attribs.href = toAbsoluteUrl(href);
127
+ }
128
+ } else if (elm.tagName === 'img') {
129
+ const {src, srcset: srcsetAttr} = elm.attribs;
130
+ if (src) {
131
+ elm.attribs.src = toAbsoluteUrl(src);
132
+ }
133
+ if (srcsetAttr) {
134
+ elm.attribs.srcset = srcset.stringify(
135
+ srcset.parse(srcsetAttr).map((props) => ({
136
+ ...props,
137
+ url: toAbsoluteUrl(props.url),
138
+ })),
139
+ );
140
+ }
141
+ }
142
+ },
143
+ );
144
+
114
145
  const feedItem: BlogFeedItem = {
115
146
  title: metadataTitle,
116
- id: link,
117
- link,
147
+ id: blogPostAbsoluteUrl,
148
+ link: blogPostAbsoluteUrl,
118
149
  date,
119
150
  description,
120
151
  // Atom feed demands the "term", while other feeds use "name"