@sota1235/remark-link-bookmark 0.0.4 → 0.0.5

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.
@@ -0,0 +1,2 @@
1
+ import debug from 'debug';
2
+ export declare const logger: debug.Debugger;
package/dist/logger.js ADDED
@@ -0,0 +1,2 @@
1
+ import debug from 'debug';
2
+ export const logger = debug('remark-link-bookmark');
@@ -0,0 +1,3 @@
1
+ import { Text, Link, PhrasingContent } from 'mdast';
2
+ export declare function isLink(child: PhrasingContent): child is Link;
3
+ export declare function isText(child: PhrasingContent): child is Text;
package/dist/mdast.js ADDED
@@ -0,0 +1,6 @@
1
+ export function isLink(child) {
2
+ return child.type === 'link';
3
+ }
4
+ export function isText(child) {
5
+ return child.type === 'text';
6
+ }
package/dist/ogp.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import ogs from 'open-graph-scraper';
2
2
  import * as fs from 'node:fs';
3
+ import { logger } from './logger';
3
4
  function isError(result) {
4
5
  return result.error;
5
6
  }
@@ -9,7 +10,7 @@ export async function fetchOgpInfo(url, { cacheFilePath, cacheFileName }) {
9
10
  const cacheData = await fs.promises.readFile(`${cacheFilePath}/${cacheFileName}`, 'utf-8');
10
11
  const cache = JSON.parse(cacheData);
11
12
  if (cache[url] !== undefined) {
12
- console.log(`Cache hit: ${url}`);
13
+ logger(`Cache hit: ${url}`);
13
14
  return cache[url];
14
15
  }
15
16
  }
@@ -38,7 +39,7 @@ export async function fetchOgpInfo(url, { cacheFilePath, cacheFileName }) {
38
39
  const cache = JSON.parse(cacheData);
39
40
  cache[url] = result;
40
41
  await fs.promises.writeFile(`${cacheFilePath}/${cacheFileName}`, JSON.stringify(cache, null, 2));
41
- console.log(`Cache updated: ${url}`);
42
+ logger(`Cache updated: ${url}`);
42
43
  }
43
44
  return result;
44
45
  }
package/dist/plugin.js CHANGED
@@ -2,40 +2,38 @@ import { visit } from 'unist-util-visit';
2
2
  import { isUrl } from './url.js';
3
3
  import { fetchOgpInfo } from './ogp.js';
4
4
  import { buildBookmarkHtml } from './view.js';
5
+ import { logger } from './logger';
5
6
  export const remarkLinkBookmark = (options) => {
6
7
  return async (tree) => {
7
8
  const transformers = [];
8
9
  visit(tree, 'paragraph', (node, index) => {
9
10
  if (index === undefined) {
10
- console.log('index is undefined');
11
+ logger('index is undefined');
11
12
  return;
12
13
  }
13
14
  if (node.children.length !== 1) {
14
- console.log('node children length is not 1');
15
+ logger('node children length is not 1');
15
16
  return;
16
17
  }
17
- const child = node.children[0];
18
- if (child.type !== 'text') {
19
- console.log('child type is not text');
20
- return;
21
- }
22
- const text = child.value;
23
- if (!isUrl(text)) {
24
- console.log('text is not url');
25
- return;
26
- }
27
- transformers.push(async () => {
28
- const ogpInfo = await fetchOgpInfo(text, options.cache ?? {});
29
- if (ogpInfo === undefined) {
18
+ visit(node, 'text', (textNode) => {
19
+ const text = textNode.value;
20
+ if (!isUrl(text)) {
21
+ logger(`text is not url: ${text}`);
30
22
  return;
31
23
  }
32
- const linkCardNode = {
33
- type: 'html',
34
- value: buildBookmarkHtml(ogpInfo, options),
35
- };
36
- tree.children.splice(index, 1, linkCardNode);
24
+ transformers.push(async () => {
25
+ const ogpInfo = await fetchOgpInfo(text, options.cache ?? {});
26
+ if (ogpInfo === undefined) {
27
+ return;
28
+ }
29
+ const linkCardNode = {
30
+ type: 'html',
31
+ value: buildBookmarkHtml(ogpInfo, options),
32
+ };
33
+ tree.children.splice(index, 1, linkCardNode);
34
+ });
35
+ return;
37
36
  });
38
- return;
39
37
  }, undefined);
40
38
  await Promise.all(transformers.map((transformer) => transformer()));
41
39
  };
package/dist/url.js CHANGED
@@ -1,10 +1,11 @@
1
+ import { logger } from './logger';
1
2
  export function isUrl(url) {
2
3
  try {
3
4
  const instance = new URL(url);
4
5
  return instance.protocol === 'https:';
5
6
  }
6
7
  catch (e) {
7
- console.log(`${url} is not a valid URL. - ${e}`);
8
+ logger(`${url} is not a valid URL. - ${e}`);
8
9
  return false;
9
10
  }
10
11
  }
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "@sota1235/remark-link-bookmark",
3
- "version": "0.0.4",
3
+ "version": "0.0.5",
4
4
  "description": "Remark plugin to convert link to bookmark component",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
7
7
  "dependencies": {
8
8
  "@types/mdast": "4.0.4",
9
9
  "clsx": "2.1.1",
10
+ "debug": "4.3.7",
10
11
  "dompurify": "3.1.7",
11
12
  "jsdom": "25.0.1",
12
13
  "open-graph-scraper": "6.8.2",
@@ -16,6 +17,7 @@
16
17
  },
17
18
  "devDependencies": {
18
19
  "@sota1235/eslint-config": "5.0.1",
20
+ "@types/debug": "4.1.12",
19
21
  "@types/dompurify": "3.0.5",
20
22
  "@types/jsdom": "21.1.7",
21
23
  "eslint": "9.14.0",