@wenyan-md/core 2.0.2 → 2.0.4

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/dist/core.js CHANGED
@@ -1293,6 +1293,7 @@ const DEFAULT_CSS_UPDATES = {
1293
1293
  ]
1294
1294
  };
1295
1295
  export {
1296
+ addFootnotes,
1296
1297
  createCssModifier,
1297
1298
  createWenyanCore,
1298
1299
  getAllGzhThemes,
package/dist/http.js ADDED
@@ -0,0 +1 @@
1
+
package/dist/publish.js CHANGED
@@ -2,49 +2,62 @@ import { JSDOM } from "jsdom";
2
2
  import { fileFromPath } from "formdata-node/file-from-path";
3
3
  import path from "node:path";
4
4
  import { stat } from "node:fs/promises";
5
- import { R as RuntimeEnv } from "./runtimeEnv-pU2mTDLR.js";
5
+ import { createWechatClient } from "./wechat.js";
6
6
  import { FormDataEncoder } from "form-data-encoder";
7
7
  import { FormData } from "formdata-node";
8
8
  import { Readable } from "node:stream";
9
- const tokenUrl = "https://api.weixin.qq.com/cgi-bin/token";
10
- const publishUrl = "https://api.weixin.qq.com/cgi-bin/draft/add";
11
- const uploadUrl = "https://api.weixin.qq.com/cgi-bin/material/add_material";
12
- function createWechatClient(adapter) {
13
- return {
14
- async fetchAccessToken(appId, appSecret) {
15
- const res = await adapter.fetch(
16
- `${tokenUrl}?grant_type=client_credential&appid=${appId}&secret=${appSecret}`
17
- );
18
- if (!res.ok) throw new Error(await res.text());
19
- return res.json();
20
- },
21
- async uploadMaterial(type, file, filename, accessToken) {
22
- const multipart = adapter.createMultipart("media", file, filename);
23
- const res = await adapter.fetch(`${uploadUrl}?access_token=${accessToken}&type=${type}`, {
24
- ...multipart,
25
- method: "POST"
26
- });
27
- const data = await res.json();
28
- if (data.errcode && data.errcode !== 0) {
29
- throw new Error(`${data.errcode}: ${data.errmsg}`);
9
+ function normalizePath(p) {
10
+ return p.replace(/\\/g, "/").replace(/\/+$/, "");
11
+ }
12
+ function isAbsolutePath(path2) {
13
+ if (!path2) return false;
14
+ const winAbsPattern = /^[a-zA-Z]:\//;
15
+ const linuxAbsPattern = /^\//;
16
+ return winAbsPattern.test(path2) || linuxAbsPattern.test(path2);
17
+ }
18
+ const RuntimeEnv = {
19
+ isContainer: !!process.env.CONTAINERIZED,
20
+ hostFilePath: normalizePath(process.env.HOST_FILE_PATH || ""),
21
+ containerFilePath: normalizePath(process.env.CONTAINER_FILE_PATH || "/mnt/host-downloads"),
22
+ resolveLocalPath(inputPath, relativeBase) {
23
+ if (!this.isContainer) {
24
+ if (relativeBase) {
25
+ return path.resolve(relativeBase, inputPath);
26
+ } else {
27
+ if (!path.isAbsolute(inputPath)) {
28
+ throw new Error(
29
+ `Invalid input: '${inputPath}'. When relativeBase is not provided, inputPath must be an absolute path.`
30
+ );
31
+ }
32
+ return path.normalize(inputPath);
33
+ }
34
+ }
35
+ let normalizedInput = normalizePath(inputPath);
36
+ relativeBase = normalizePath(relativeBase || "");
37
+ if (relativeBase) {
38
+ if (!isAbsolutePath(normalizedInput)) {
39
+ normalizedInput = relativeBase + (normalizedInput.startsWith("/") ? "" : "/") + normalizedInput;
40
+ }
41
+ } else {
42
+ if (!isAbsolutePath(normalizedInput)) {
43
+ throw new Error(
44
+ `Invalid input: '${inputPath}'. When relativeBase is not provided, inputPath must be an absolute path.`
45
+ );
46
+ }
47
+ }
48
+ if (normalizedInput.startsWith(this.hostFilePath)) {
49
+ let relativePart = normalizedInput.slice(this.hostFilePath.length);
50
+ if (relativePart && !relativePart.startsWith("/")) {
51
+ return normalizedInput;
30
52
  }
31
- if (data.url?.startsWith("http://")) {
32
- data.url = data.url.replace(/^http:\/\//i, "https://");
53
+ if (!relativePart.startsWith("/")) {
54
+ relativePart = "/" + relativePart;
33
55
  }
34
- return data;
35
- },
36
- async publishArticle(title, content, thumbMediaId, accessToken) {
37
- const res = await adapter.fetch(`${publishUrl}?access_token=${accessToken}`, {
38
- method: "POST",
39
- body: JSON.stringify({
40
- articles: [{ title, content, thumb_media_id: thumbMediaId }]
41
- })
42
- });
43
- if (!res.ok) throw new Error(await res.text());
44
- return res.json();
56
+ return this.containerFilePath + relativePart;
45
57
  }
46
- };
47
- }
58
+ return normalizedInput;
59
+ }
60
+ };
48
61
  const nodeHttpAdapter = {
49
62
  fetch,
50
63
  createMultipart(field, file, filename) {
@@ -30,3 +30,4 @@ export { getMacStyleCss, registerMacStyle } from "./theme/macStyleRegistry.js";
30
30
  export * from "./platform/medium.js";
31
31
  export * from "./platform/zhihu.js";
32
32
  export * from "./platform/toutiao.js";
33
+ export { addFootnotes } from "./renderer/footnotesRender.js";
@@ -8,4 +8,3 @@ export interface StyledContent {
8
8
  export declare function renderStyledContent(content: string, options?: ApplyStylesOptions): Promise<StyledContent>;
9
9
  export declare function getGzhContent(content: string, themeId: string, hlThemeId: string, isMacStyle?: boolean, isAddFootnote?: boolean): Promise<StyledContent>;
10
10
  export * from "./configStore.js";
11
- export * from "./runtimeEnv.js";
@@ -4,3 +4,4 @@ export declare function createWechatClient(adapter: HttpAdapter): {
4
4
  uploadMaterial(type: string, file: Blob, filename: string, accessToken: string): Promise<any>;
5
5
  publishArticle(title: string, content: string, thumbMediaId: string, accessToken: string): Promise<any>;
6
6
  };
7
+ export type WechatClient = ReturnType<typeof createWechatClient>;
package/dist/wechat.js ADDED
@@ -0,0 +1,42 @@
1
+ const tokenUrl = "https://api.weixin.qq.com/cgi-bin/token";
2
+ const publishUrl = "https://api.weixin.qq.com/cgi-bin/draft/add";
3
+ const uploadUrl = "https://api.weixin.qq.com/cgi-bin/material/add_material";
4
+ function createWechatClient(adapter) {
5
+ return {
6
+ async fetchAccessToken(appId, appSecret) {
7
+ const res = await adapter.fetch(
8
+ `${tokenUrl}?grant_type=client_credential&appid=${appId}&secret=${appSecret}`
9
+ );
10
+ if (!res.ok) throw new Error(await res.text());
11
+ return res.json();
12
+ },
13
+ async uploadMaterial(type, file, filename, accessToken) {
14
+ const multipart = adapter.createMultipart("media", file, filename);
15
+ const res = await adapter.fetch(`${uploadUrl}?access_token=${accessToken}&type=${type}`, {
16
+ ...multipart,
17
+ method: "POST"
18
+ });
19
+ const data = await res.json();
20
+ if (data.errcode && data.errcode !== 0) {
21
+ throw new Error(`${data.errcode}: ${data.errmsg}`);
22
+ }
23
+ if (data.url?.startsWith("http://")) {
24
+ data.url = data.url.replace(/^http:\/\//i, "https://");
25
+ }
26
+ return data;
27
+ },
28
+ async publishArticle(title, content, thumbMediaId, accessToken) {
29
+ const res = await adapter.fetch(`${publishUrl}?access_token=${accessToken}`, {
30
+ method: "POST",
31
+ body: JSON.stringify({
32
+ articles: [{ title, content, thumb_media_id: thumbMediaId }]
33
+ })
34
+ });
35
+ if (!res.ok) throw new Error(await res.text());
36
+ return res.json();
37
+ }
38
+ };
39
+ }
40
+ export {
41
+ createWechatClient
42
+ };
package/dist/wrapper.js CHANGED
@@ -3,7 +3,6 @@ import { createWenyanCore } from "./core.js";
3
3
  import path from "node:path";
4
4
  import os from "node:os";
5
5
  import fs from "node:fs";
6
- import { R } from "./runtimeEnv-pU2mTDLR.js";
7
6
  const defaultConfig = {};
8
7
  const configDir = process.env.APPDATA ? path.join(process.env.APPDATA, "wenyan-md") : path.join(os.homedir(), ".config", "wenyan-md");
9
8
  const configPath = path.join(configDir, "config.json");
@@ -112,7 +111,6 @@ async function getGzhContent(content, themeId, hlThemeId, isMacStyle = true, isA
112
111
  });
113
112
  }
114
113
  export {
115
- R as RuntimeEnv,
116
114
  configDir,
117
115
  configPath,
118
116
  configStore,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wenyan-md/core",
3
- "version": "2.0.2",
3
+ "version": "2.0.4",
4
4
  "description": "Core library for Wenyan markdown rendering & publishing",
5
5
  "author": "Lei <caol64@gmail.com> (https://github.com/caol64)",
6
6
  "license": "Apache-2.0",
@@ -28,16 +28,23 @@
28
28
  "exports": {
29
29
  ".": {
30
30
  "import": "./dist/core.js",
31
- "types": "./dist/types/core/index.d.ts",
32
- "browser": "./dist/browser/wenyan-core.js"
31
+ "types": "./dist/types/core/index.d.ts"
33
32
  },
34
33
  "./publish": {
35
34
  "import": "./dist/publish.js",
36
- "types": "./dist/types/wechat/publish.d.ts"
35
+ "types": "./dist/types/node/publish.d.ts"
37
36
  },
38
37
  "./wrapper": {
39
38
  "import": "./dist/wrapper.js",
40
39
  "types": "./dist/types/node/wrapper.d.ts"
40
+ },
41
+ "./wechat": {
42
+ "import": "./dist/wechat.js",
43
+ "types": "./dist/types/wechat.d.ts"
44
+ },
45
+ "./http": {
46
+ "import": "./dist/http.js",
47
+ "types": "./dist/types/http.d.ts"
41
48
  }
42
49
  },
43
50
  "devDependencies": {
@@ -1,56 +0,0 @@
1
- import path from "node:path";
2
- function normalizePath(p) {
3
- return p.replace(/\\/g, "/").replace(/\/+$/, "");
4
- }
5
- function isAbsolutePath(path2) {
6
- if (!path2) return false;
7
- const winAbsPattern = /^[a-zA-Z]:\//;
8
- const linuxAbsPattern = /^\//;
9
- return winAbsPattern.test(path2) || linuxAbsPattern.test(path2);
10
- }
11
- const RuntimeEnv = {
12
- isContainer: !!process.env.CONTAINERIZED,
13
- hostFilePath: normalizePath(process.env.HOST_FILE_PATH || ""),
14
- containerFilePath: normalizePath(process.env.CONTAINER_FILE_PATH || "/mnt/host-downloads"),
15
- resolveLocalPath(inputPath, relativeBase) {
16
- if (!this.isContainer) {
17
- if (relativeBase) {
18
- return path.resolve(relativeBase, inputPath);
19
- } else {
20
- if (!path.isAbsolute(inputPath)) {
21
- throw new Error(
22
- `Invalid input: '${inputPath}'. When relativeBase is not provided, inputPath must be an absolute path.`
23
- );
24
- }
25
- return path.normalize(inputPath);
26
- }
27
- }
28
- let normalizedInput = normalizePath(inputPath);
29
- relativeBase = normalizePath(relativeBase || "");
30
- if (relativeBase) {
31
- if (!isAbsolutePath(normalizedInput)) {
32
- normalizedInput = relativeBase + (normalizedInput.startsWith("/") ? "" : "/") + normalizedInput;
33
- }
34
- } else {
35
- if (!isAbsolutePath(normalizedInput)) {
36
- throw new Error(
37
- `Invalid input: '${inputPath}'. When relativeBase is not provided, inputPath must be an absolute path.`
38
- );
39
- }
40
- }
41
- if (normalizedInput.startsWith(this.hostFilePath)) {
42
- let relativePart = normalizedInput.slice(this.hostFilePath.length);
43
- if (relativePart && !relativePart.startsWith("/")) {
44
- return normalizedInput;
45
- }
46
- if (!relativePart.startsWith("/")) {
47
- relativePart = "/" + relativePart;
48
- }
49
- return this.containerFilePath + relativePart;
50
- }
51
- return normalizedInput;
52
- }
53
- };
54
- export {
55
- RuntimeEnv as R
56
- };
File without changes
File without changes