@jam-comments/server-utilities 4.3.2 → 4.3.3

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.
@@ -4,14 +4,10 @@ exports.injectSchema = void 0;
4
4
  const utils_1 = require("./utils");
5
5
  function injectSchema(markup, schema) {
6
6
  const commentSchema = markup.match(/<div jc-data="jcSchema" data-schema="(.*)"><\/div>/)?.[1];
7
- if (!commentSchema) {
8
- return markup;
7
+ const json = commentSchema ? (0, utils_1.parseJson)((0, utils_1.unescapeHTML)(commentSchema)) : null;
8
+ if (json) {
9
+ schema.comment = json;
9
10
  }
10
- const json = (0, utils_1.parseJson)((0, utils_1.unescapeHTML)(commentSchema));
11
- if (!json) {
12
- return markup;
13
- }
14
- schema.comment = json;
15
11
  return markup
16
12
  .replace("<!-- JC:SCHEMA -->", `<script type="application/ld+json">${JSON.stringify(schema)}</script>`)
17
13
  .replace(/<div jc-data="jcSchema" data-schema=".*"><\/div>(?:\n+)?/, "");
@@ -19,30 +19,36 @@ const injectSchema_1 = require("./injectSchema");
19
19
  <script type="application/ld+json">{"@context":"http://schema.org","@type":"BlogPosting","comment":[{"@context":"https://schema.org","@type":"Comment","name":"Alexis","dateCreated":"2022-10-28T10:41:49+00:00","url":"https://macarthur.me/posts/dynamic-routing#comment-133"}]}</script>
20
20
  </div>`);
21
21
  });
22
- (0, vitest_1.it)("returns markup if schema is not found", () => {
22
+ (0, vitest_1.it)("injects schema even if comment schema isn't found.", () => {
23
23
  const markup = `
24
24
  <div id="jcComments">
25
- <!-- JC:SCHEMA -->
25
+ <!-- JC:SCHEMA -->
26
26
  </div>`;
27
27
  const blogPostSchema = {
28
28
  "@context": "http://schema.org",
29
29
  "@type": "BlogPosting",
30
30
  };
31
31
  const result = (0, injectSchema_1.injectSchema)(markup, blogPostSchema);
32
- (0, vitest_1.expect)(result).toEqual(markup);
32
+ (0, vitest_1.expect)(result).toEqual(`
33
+ <div id="jcComments">
34
+ <script type="application/ld+json">{"@context":"http://schema.org","@type":"BlogPosting"}</script>
35
+ </div>`);
33
36
  });
34
- (0, vitest_1.it)("returns markup if schema is not valid JSON", () => {
37
+ (0, vitest_1.it)("injects markup even if json isn't valid", () => {
35
38
  const markup = `
36
39
  <div id="jcComments">
37
- <div jc-data="jcSchema" data-schema="not-valid-json"></div>
38
- <!-- JC:SCHEMA -->
40
+ <div jc-data="jcSchema" data-schema="not-valid-json"></div>
41
+ <!-- JC:SCHEMA -->
39
42
  </div>`;
40
43
  const blogPostSchema = {
41
44
  "@context": "http://schema.org",
42
45
  "@type": "BlogPosting",
43
46
  };
44
47
  const result = (0, injectSchema_1.injectSchema)(markup, blogPostSchema);
45
- (0, vitest_1.expect)(result).toEqual(markup);
48
+ (0, vitest_1.expect)(result).toEqual(`
49
+ <div id="jcComments">
50
+ <script type="application/ld+json">{"@context":"http://schema.org","@type":"BlogPosting"}</script>
51
+ </div>`);
46
52
  });
47
53
  (0, vitest_1.it)("injects even when JSON is not HTML-encoded", () => {
48
54
  const markup = `
@@ -1,14 +1,10 @@
1
1
  import { parseJson, unescapeHTML } from "./utils";
2
2
  export function injectSchema(markup, schema) {
3
3
  const commentSchema = markup.match(/<div jc-data="jcSchema" data-schema="(.*)"><\/div>/)?.[1];
4
- if (!commentSchema) {
5
- return markup;
4
+ const json = commentSchema ? parseJson(unescapeHTML(commentSchema)) : null;
5
+ if (json) {
6
+ schema.comment = json;
6
7
  }
7
- const json = parseJson(unescapeHTML(commentSchema));
8
- if (!json) {
9
- return markup;
10
- }
11
- schema.comment = json;
12
8
  return markup
13
9
  .replace("<!-- JC:SCHEMA -->", `<script type="application/ld+json">${JSON.stringify(schema)}</script>`)
14
10
  .replace(/<div jc-data="jcSchema" data-schema=".*"><\/div>(?:\n+)?/, "");
@@ -17,30 +17,36 @@ it("injects schema into markup", () => {
17
17
  <script type="application/ld+json">{"@context":"http://schema.org","@type":"BlogPosting","comment":[{"@context":"https://schema.org","@type":"Comment","name":"Alexis","dateCreated":"2022-10-28T10:41:49+00:00","url":"https://macarthur.me/posts/dynamic-routing#comment-133"}]}</script>
18
18
  </div>`);
19
19
  });
20
- it("returns markup if schema is not found", () => {
20
+ it("injects schema even if comment schema isn't found.", () => {
21
21
  const markup = `
22
22
  <div id="jcComments">
23
- <!-- JC:SCHEMA -->
23
+ <!-- JC:SCHEMA -->
24
24
  </div>`;
25
25
  const blogPostSchema = {
26
26
  "@context": "http://schema.org",
27
27
  "@type": "BlogPosting",
28
28
  };
29
29
  const result = injectSchema(markup, blogPostSchema);
30
- expect(result).toEqual(markup);
30
+ expect(result).toEqual(`
31
+ <div id="jcComments">
32
+ <script type="application/ld+json">{"@context":"http://schema.org","@type":"BlogPosting"}</script>
33
+ </div>`);
31
34
  });
32
- it("returns markup if schema is not valid JSON", () => {
35
+ it("injects markup even if json isn't valid", () => {
33
36
  const markup = `
34
37
  <div id="jcComments">
35
- <div jc-data="jcSchema" data-schema="not-valid-json"></div>
36
- <!-- JC:SCHEMA -->
38
+ <div jc-data="jcSchema" data-schema="not-valid-json"></div>
39
+ <!-- JC:SCHEMA -->
37
40
  </div>`;
38
41
  const blogPostSchema = {
39
42
  "@context": "http://schema.org",
40
43
  "@type": "BlogPosting",
41
44
  };
42
45
  const result = injectSchema(markup, blogPostSchema);
43
- expect(result).toEqual(markup);
46
+ expect(result).toEqual(`
47
+ <div id="jcComments">
48
+ <script type="application/ld+json">{"@context":"http://schema.org","@type":"BlogPosting"}</script>
49
+ </div>`);
44
50
  });
45
51
  it("injects even when JSON is not HTML-encoded", () => {
46
52
  const markup = `
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jam-comments/server-utilities",
3
- "version": "4.3.2",
3
+ "version": "4.3.3",
4
4
  "description": "Various JavaScript utilities for JamComments.",
5
5
  "main": "dist/cjs/index.js",
6
6
  "types": "dist/types/index.d.ts",