@alauda/doom 2.2.0 → 2.3.1
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/lib/cli/translate.d.ts +1 -3
- package/lib/cli/translate.js +2 -56
- package/lib/global/Intelligence/AIAssistant/index.js +44 -14
- package/lib/global/Intelligence/AIAssistant/utils.d.ts +12 -1
- package/lib/global/Intelligence/AIAssistant/utils.js +113 -11
- package/lib/remark-lint/file-naming.d.ts +2 -0
- package/lib/remark-lint/file-naming.js +25 -0
- package/lib/remark-lint/heading-anchor-format.d.ts +2 -0
- package/lib/remark-lint/heading-anchor-format.js +108 -0
- package/lib/remark-lint/index.d.ts +2 -0
- package/lib/remark-lint/index.js +2 -0
- package/lib/remarkrc.js +2 -1
- package/package.json +1 -1
package/lib/cli/translate.d.ts
CHANGED
|
@@ -16,10 +16,8 @@ export interface InternalTranslateOptions extends TranslateOptions {
|
|
|
16
16
|
sourceContent: string;
|
|
17
17
|
target: Language;
|
|
18
18
|
additionalPrompts?: string;
|
|
19
|
-
isChunk?: boolean;
|
|
20
19
|
}
|
|
21
|
-
export declare const
|
|
22
|
-
export declare const translate: ({ source, sourceContent, target, systemPrompt, userPrompt, additionalPrompts, isChunk, }: InternalTranslateOptions) => Promise<string>;
|
|
20
|
+
export declare const translate: ({ source, sourceContent, target, systemPrompt, userPrompt, additionalPrompts, }: InternalTranslateOptions) => Promise<string>;
|
|
23
21
|
export interface TranslateCommandOptions {
|
|
24
22
|
source: Language;
|
|
25
23
|
target: Language;
|
package/lib/cli/translate.js
CHANGED
|
@@ -61,11 +61,6 @@ You are a professional technical documentation engineer, skilled in writing high
|
|
|
61
61
|
<% if (terms) { %>
|
|
62
62
|
<%- terms %>
|
|
63
63
|
<% } %>
|
|
64
|
-
<% if (isChunk) { %>
|
|
65
|
-
## Chunk Translation Notice
|
|
66
|
-
This is part of a larger document that has been split into smaller chunks for translation. Please translate this chunk as if it's part of a continuous document, maintaining consistency with the overall document style and context.
|
|
67
|
-
<% } %>
|
|
68
|
-
|
|
69
64
|
<% if (userPrompt || additionalPrompts) { %>
|
|
70
65
|
## Additional Requirements
|
|
71
66
|
These are additional requirements for the translation. They should be met along with the baseline requirements, and in case of any conflict, the baseline requirements should take precedence.
|
|
@@ -152,55 +147,7 @@ function getTitleTranslation(title, sourceLang, targetLang) {
|
|
|
152
147
|
}
|
|
153
148
|
return null;
|
|
154
149
|
}
|
|
155
|
-
|
|
156
|
-
const lines = content.split('\n');
|
|
157
|
-
const chunks = [];
|
|
158
|
-
let currentChunk = [];
|
|
159
|
-
let currentSize = 0;
|
|
160
|
-
for (const line of lines) {
|
|
161
|
-
const lineSize = Buffer.byteLength(line + '\n', 'utf8');
|
|
162
|
-
// If adding this line would exceed the chunk size, and we have content in current chunk
|
|
163
|
-
if (currentSize + lineSize > maxChunkSize && currentChunk.length > 0) {
|
|
164
|
-
chunks.push(currentChunk.join('\n'));
|
|
165
|
-
currentChunk = [line];
|
|
166
|
-
currentSize = lineSize;
|
|
167
|
-
}
|
|
168
|
-
else {
|
|
169
|
-
currentChunk.push(line);
|
|
170
|
-
currentSize += lineSize;
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
// Add the last chunk if it has content
|
|
174
|
-
if (currentChunk.length > 0) {
|
|
175
|
-
chunks.push(currentChunk.join('\n'));
|
|
176
|
-
}
|
|
177
|
-
return chunks;
|
|
178
|
-
}
|
|
179
|
-
export const translateWithChunks = async (options) => {
|
|
180
|
-
const { sourceContent } = options;
|
|
181
|
-
const maxChunkSize = 60 * 1024;
|
|
182
|
-
const contentSize = Buffer.byteLength(sourceContent, 'utf8');
|
|
183
|
-
if (contentSize <= maxChunkSize) {
|
|
184
|
-
return translate(options);
|
|
185
|
-
}
|
|
186
|
-
logger.info(`Content size (${Math.round(contentSize / 1024)}KB) exceeds limit, splitting into chunks...`);
|
|
187
|
-
const chunks = splitContentIntoChunks(sourceContent, maxChunkSize);
|
|
188
|
-
logger.info(`Split content into ${chunks.length} chunks`);
|
|
189
|
-
const translatedChunks = [];
|
|
190
|
-
for (let i = 0; i < chunks.length; i++) {
|
|
191
|
-
logger.info(`Translating chunk ${i + 1}/${chunks.length}...`);
|
|
192
|
-
const translatedChunk = await translate({
|
|
193
|
-
...options,
|
|
194
|
-
sourceContent: chunks[i],
|
|
195
|
-
isChunk: true,
|
|
196
|
-
});
|
|
197
|
-
translatedChunks.push(translatedChunk);
|
|
198
|
-
}
|
|
199
|
-
const result = translatedChunks.join('\n');
|
|
200
|
-
logger.info(`Successfully translated ${chunks.length} chunks`);
|
|
201
|
-
return result;
|
|
202
|
-
};
|
|
203
|
-
export const translate = async ({ source, sourceContent, target, systemPrompt, userPrompt = '', additionalPrompts = '', isChunk = false, }) => {
|
|
150
|
+
export const translate = async ({ source, sourceContent, target, systemPrompt, userPrompt = '', additionalPrompts = '', }) => {
|
|
204
151
|
if (!openai) {
|
|
205
152
|
openai = new OpenAI({
|
|
206
153
|
baseURL: process.env.ALAUDA_OPENAI_BASE_URL,
|
|
@@ -229,7 +176,6 @@ export const translate = async ({ source, sourceContent, target, systemPrompt, u
|
|
|
229
176
|
additionalPrompts: additionalPrompts,
|
|
230
177
|
terms,
|
|
231
178
|
titleTranslationPrompt,
|
|
232
|
-
isChunk,
|
|
233
179
|
}, { async: true });
|
|
234
180
|
logger.debug('Final system prompt:\n', finalSystemPrompt);
|
|
235
181
|
const stream = await openai.chat.completions.create({
|
|
@@ -390,7 +336,7 @@ export const translateCommand = new Command('translate')
|
|
|
390
336
|
...ast,
|
|
391
337
|
children: ast.children.map((it) => normalizeImgSrc(it, normalizeImgSrcOptions)),
|
|
392
338
|
});
|
|
393
|
-
targetContent = await
|
|
339
|
+
targetContent = await translate({
|
|
394
340
|
...config.translate,
|
|
395
341
|
source,
|
|
396
342
|
sourceContent: normalizedSourceContent,
|
|
@@ -9,13 +9,12 @@ import { Chat } from "./Chat/index.js";
|
|
|
9
9
|
import { Preamble } from "./Preamble/index.js";
|
|
10
10
|
import { ResizableUserInput } from "./ResizableUserInput/index.js";
|
|
11
11
|
import { Thinking } from "./Thinking.js";
|
|
12
|
-
import { parseStreamContent } from "./utils.js";
|
|
12
|
+
import { consumeSSEEvents, getAnswerDelta, parseStreamContent, } from "./utils.js";
|
|
13
13
|
import CloseIcon from '@alauda/doom/assets/close.svg?react';
|
|
14
14
|
import LogoutIcon from '@alauda/doom/assets/logout.svg?react';
|
|
15
15
|
import NewChatIcon from '@alauda/doom/assets/new-chat.svg?react';
|
|
16
16
|
import { useMemoizedFn, useTranslation } from '@alauda/doom/runtime';
|
|
17
17
|
import classes from '@alauda/doom/styles/ai-assistant.module.scss';
|
|
18
|
-
let textDecoder;
|
|
19
18
|
export const AIAssistant = ({ open, onOpenChange }) => {
|
|
20
19
|
const t = useTranslation();
|
|
21
20
|
const { authInfo, setAuthBasic } = useCloudAuth();
|
|
@@ -92,7 +91,7 @@ export const AIAssistant = ({ open, onOpenChange }) => {
|
|
|
92
91
|
sessionIdRef.current = session_id;
|
|
93
92
|
}
|
|
94
93
|
const sessionId = sessionIdRef.current;
|
|
95
|
-
const res = await xfetch('/smart/api/
|
|
94
|
+
const res = await xfetch('/smart/api/smart_answer_with_search', {
|
|
96
95
|
type: null,
|
|
97
96
|
method: ApiMethod.POST,
|
|
98
97
|
body: {
|
|
@@ -100,30 +99,61 @@ export const AIAssistant = ({ open, onOpenChange }) => {
|
|
|
100
99
|
session_id: sessionId,
|
|
101
100
|
},
|
|
102
101
|
});
|
|
103
|
-
textDecoder
|
|
102
|
+
const textDecoder = new TextDecoder();
|
|
104
103
|
let text = '';
|
|
104
|
+
let sseBuffer = '';
|
|
105
|
+
let sseEvent;
|
|
105
106
|
for await (const chunk_ of res.body) {
|
|
106
107
|
if (sessionId !== sessionIdRef.current) {
|
|
107
108
|
break;
|
|
108
109
|
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
const
|
|
116
|
-
|
|
117
|
-
!
|
|
118
|
-
|
|
110
|
+
sseBuffer += textDecoder.decode(chunk_, { stream: true });
|
|
111
|
+
const result = consumeSSEEvents(sseBuffer, sseEvent);
|
|
112
|
+
const { events, remainder } = result;
|
|
113
|
+
sseBuffer = remainder;
|
|
114
|
+
sseEvent = result.event;
|
|
115
|
+
let updated = false;
|
|
116
|
+
for (const event of events) {
|
|
117
|
+
const delta = getAnswerDelta(event);
|
|
118
|
+
if (!delta) {
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
text += delta;
|
|
122
|
+
updated = true;
|
|
123
|
+
}
|
|
124
|
+
if (!updated) {
|
|
119
125
|
continue;
|
|
120
126
|
}
|
|
127
|
+
const parsed = parseStreamContent(text);
|
|
121
128
|
flushMessages((messages) => [
|
|
122
129
|
...messages.slice(0, index),
|
|
123
130
|
{ ...messages[index], ...parsed },
|
|
124
131
|
...messages.slice(index + 1),
|
|
125
132
|
]);
|
|
126
133
|
}
|
|
134
|
+
if (sessionId !== sessionIdRef.current) {
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
sseBuffer += textDecoder.decode();
|
|
138
|
+
const { events } = consumeSSEEvents(`${sseBuffer}\n`, sseEvent);
|
|
139
|
+
let updated = false;
|
|
140
|
+
for (const event of events) {
|
|
141
|
+
const delta = getAnswerDelta(event);
|
|
142
|
+
if (!delta) {
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
text += delta;
|
|
146
|
+
updated = true;
|
|
147
|
+
}
|
|
148
|
+
if (!updated) {
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
const parsed = parseStreamContent(text);
|
|
152
|
+
flushMessages((messages) => [
|
|
153
|
+
...messages.slice(0, index),
|
|
154
|
+
{ ...messages[index], ...parsed },
|
|
155
|
+
...messages.slice(index + 1),
|
|
156
|
+
]);
|
|
127
157
|
};
|
|
128
158
|
const [loading, setLoading] = useState(false);
|
|
129
159
|
const onSend = useMemoizedFn(async (content) => {
|
|
@@ -1,8 +1,19 @@
|
|
|
1
1
|
import type { RefDoc } from './types.js';
|
|
2
|
+
export interface SSEEvent {
|
|
3
|
+
data: string;
|
|
4
|
+
event?: string;
|
|
5
|
+
}
|
|
6
|
+
export interface SSEConsumeResult {
|
|
7
|
+
event?: string;
|
|
8
|
+
events: SSEEvent[];
|
|
9
|
+
remainder: string;
|
|
10
|
+
}
|
|
2
11
|
export declare const unicodeToString: (unicodeStr: string) => string;
|
|
3
12
|
export declare const removePrefix: (prefix: string | null | undefined, text: string) => string;
|
|
13
|
+
export declare const consumeSSEEvents: (buffer: string, event?: string) => SSEConsumeResult;
|
|
14
|
+
export declare const getAnswerDelta: ({ event, data }: SSEEvent) => string;
|
|
4
15
|
export declare function parseStreamContent(text: string): {
|
|
5
16
|
refDocs: RefDoc[];
|
|
6
|
-
thinkingProcess: string |
|
|
17
|
+
thinkingProcess: string | undefined;
|
|
7
18
|
content: string;
|
|
8
19
|
};
|
|
@@ -1,11 +1,109 @@
|
|
|
1
1
|
// based on https://gitlab-ce.alauda.cn/aml/smart-doc/-/blob/master/frontend/src/app/utils/unicode.ts
|
|
2
2
|
import { isObject } from 'es-toolkit/compat';
|
|
3
|
+
const IGNORED_SSE_EVENT_TYPES = /(?:^|[._-])(?:metadata|trace|updates?)(?:$|[._-])/i;
|
|
4
|
+
const STREAM_TEXT_KEYS = ['delta', 'text', 'content', 'answer', 'message'];
|
|
3
5
|
export const unicodeToString = (unicodeStr) => unicodeStr.replace(/\\u([0-9a-fA-F]{4})/g, (_match, p1) => String.fromCharCode(Number.parseInt(p1, 16)));
|
|
6
|
+
const isRecord = (value) => isObject(value) && !Array.isArray(value);
|
|
4
7
|
export const removePrefix = (prefix, text) => (prefix && text.startsWith(prefix) ? text.slice(prefix.length) : text);
|
|
8
|
+
const parseSSEFieldValue = (line, field) => {
|
|
9
|
+
const value = line.slice(field.length + 1);
|
|
10
|
+
return value.startsWith(' ') ? value.slice(1) : value;
|
|
11
|
+
};
|
|
12
|
+
const getStringValue = (value) => {
|
|
13
|
+
if (typeof value === 'string') {
|
|
14
|
+
return value;
|
|
15
|
+
}
|
|
16
|
+
if (Array.isArray(value)) {
|
|
17
|
+
const text = value.map((item) => getStringValue(item) || '').join('');
|
|
18
|
+
return text || undefined;
|
|
19
|
+
}
|
|
20
|
+
if (!isRecord(value)) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
for (const key of STREAM_TEXT_KEYS) {
|
|
24
|
+
const text = getStringValue(value[key]);
|
|
25
|
+
if (text) {
|
|
26
|
+
return text;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
const getChoicesDelta = (choices) => {
|
|
31
|
+
if (!Array.isArray(choices)) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
const text = choices.map((choice) => getStringValue(choice) || '').join('');
|
|
35
|
+
return text || undefined;
|
|
36
|
+
};
|
|
37
|
+
export const consumeSSEEvents = (buffer, event) => {
|
|
38
|
+
const events = [];
|
|
39
|
+
let remaining = buffer;
|
|
40
|
+
let currentEvent = event;
|
|
41
|
+
let lineBoundary = remaining.match(/\r?\n/);
|
|
42
|
+
while (lineBoundary?.index != null) {
|
|
43
|
+
const line = remaining.slice(0, lineBoundary.index);
|
|
44
|
+
remaining = remaining.slice(lineBoundary.index + lineBoundary[0].length);
|
|
45
|
+
if (!line) {
|
|
46
|
+
currentEvent = undefined;
|
|
47
|
+
}
|
|
48
|
+
else if (line.startsWith(':')) {
|
|
49
|
+
//
|
|
50
|
+
}
|
|
51
|
+
else if (line.startsWith('event:')) {
|
|
52
|
+
currentEvent = parseSSEFieldValue(line, 'event');
|
|
53
|
+
}
|
|
54
|
+
else if (line.startsWith('data:')) {
|
|
55
|
+
events.push({
|
|
56
|
+
event: currentEvent,
|
|
57
|
+
data: parseSSEFieldValue(line, 'data'),
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
lineBoundary = remaining.match(/\r?\n/);
|
|
61
|
+
}
|
|
62
|
+
return {
|
|
63
|
+
event: currentEvent,
|
|
64
|
+
events,
|
|
65
|
+
remainder: remaining,
|
|
66
|
+
};
|
|
67
|
+
};
|
|
68
|
+
export const getAnswerDelta = ({ event, data }) => {
|
|
69
|
+
if (!data || data === '[DONE]') {
|
|
70
|
+
return '';
|
|
71
|
+
}
|
|
72
|
+
if (event && IGNORED_SSE_EVENT_TYPES.test(event)) {
|
|
73
|
+
return '';
|
|
74
|
+
}
|
|
75
|
+
try {
|
|
76
|
+
const payload = JSON.parse(data);
|
|
77
|
+
if (typeof payload === 'string') {
|
|
78
|
+
return payload;
|
|
79
|
+
}
|
|
80
|
+
if (!isRecord(payload)) {
|
|
81
|
+
return '';
|
|
82
|
+
}
|
|
83
|
+
const type = typeof payload.type === 'string' ? payload.type : undefined;
|
|
84
|
+
if (type && IGNORED_SSE_EVENT_TYPES.test(type)) {
|
|
85
|
+
return '';
|
|
86
|
+
}
|
|
87
|
+
const choiceDelta = getChoicesDelta(payload.choices);
|
|
88
|
+
if (choiceDelta) {
|
|
89
|
+
return choiceDelta;
|
|
90
|
+
}
|
|
91
|
+
for (const key of STREAM_TEXT_KEYS) {
|
|
92
|
+
const text = getStringValue(payload[key]);
|
|
93
|
+
if (text) {
|
|
94
|
+
return text;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return '';
|
|
98
|
+
}
|
|
99
|
+
catch {
|
|
100
|
+
return data;
|
|
101
|
+
}
|
|
102
|
+
};
|
|
5
103
|
export function parseStreamContent(text) {
|
|
6
|
-
const
|
|
7
|
-
const docsReferences =
|
|
8
|
-
? unicodeToString(
|
|
104
|
+
const docsMatches = [...text.matchAll(/<docs>([\s\S]*?)<\/docs>/g)];
|
|
105
|
+
const docsReferences = docsMatches.length
|
|
106
|
+
? unicodeToString(docsMatches.at(-1)[1])
|
|
9
107
|
: '[]';
|
|
10
108
|
let refDocs = [];
|
|
11
109
|
try {
|
|
@@ -14,16 +112,20 @@ export function parseStreamContent(text) {
|
|
|
14
112
|
catch {
|
|
15
113
|
//
|
|
16
114
|
}
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
115
|
+
const thinkMatches = [...text.matchAll(/<think>([\s\S]*?)<\/think>/g)];
|
|
116
|
+
let thinkingProcess = thinkMatches.at(-1)?.[1];
|
|
117
|
+
if (!thinkingProcess) {
|
|
118
|
+
const openThinkMatch = text.match(/<think>([\s\S]*)$/);
|
|
119
|
+
thinkingProcess = openThinkMatch?.[1];
|
|
22
120
|
}
|
|
23
|
-
const
|
|
121
|
+
const content = text
|
|
122
|
+
.replace(/<docs>[\s\S]*?<\/docs>/g, '')
|
|
123
|
+
.replace(/<think>[\s\S]*?<\/think>/g, '')
|
|
124
|
+
.replace(/<docs>[\s\S]*$/g, '')
|
|
125
|
+
.replace(/<think>[\s\S]*$/g, '');
|
|
24
126
|
return {
|
|
25
127
|
refDocs,
|
|
26
|
-
thinkingProcess,
|
|
27
|
-
content: unicodeToString(
|
|
128
|
+
thinkingProcess: thinkingProcess && unicodeToString(thinkingProcess),
|
|
129
|
+
content: unicodeToString(content),
|
|
28
130
|
};
|
|
29
131
|
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { lintRule } from 'unified-lint-rule';
|
|
3
|
+
import { getConfig } from "./utils.js";
|
|
4
|
+
const VALID_NAME_PATTERN = /^_?[a-z0-9]+(?:_[a-z0-9]+)*$/;
|
|
5
|
+
export const fileNaming = lintRule('doom-lint:file-naming', async (_root, vfile) => {
|
|
6
|
+
if (!vfile.path) {
|
|
7
|
+
return;
|
|
8
|
+
}
|
|
9
|
+
const { config } = await getConfig();
|
|
10
|
+
const relativePath = path.relative(config.root, vfile.path);
|
|
11
|
+
const prefix = config.lang ? config.lang + '/' : '';
|
|
12
|
+
// Ignore files outside the root directory
|
|
13
|
+
if (relativePath.startsWith('..') ||
|
|
14
|
+
relativePath === `${prefix}index.md` ||
|
|
15
|
+
relativePath === `${prefix}index.mdx` ||
|
|
16
|
+
relativePath.startsWith(`${prefix}apis/`)) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
const stem = vfile.stem;
|
|
20
|
+
const isIndex = stem === 'index';
|
|
21
|
+
const basename = isIndex ? path.basename(vfile.dirname) : stem;
|
|
22
|
+
if (!VALID_NAME_PATTERN.test(basename)) {
|
|
23
|
+
vfile.message(`${isIndex ? 'Directory name' : 'Filename'} "${basename}" does not match the required pattern: lowercase letters, numbers, and underscores only, must not end with underscore`);
|
|
24
|
+
}
|
|
25
|
+
});
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { lintRule } from 'unified-lint-rule';
|
|
2
|
+
import { visitParents } from 'unist-util-visit-parents';
|
|
3
|
+
const getHtmlId = (value) => {
|
|
4
|
+
const match = /\sid\s*=\s*(["'])(.*?)\1/iu.exec(value);
|
|
5
|
+
if (!match) {
|
|
6
|
+
return;
|
|
7
|
+
}
|
|
8
|
+
return match[2];
|
|
9
|
+
};
|
|
10
|
+
const getHtmlIdBearingElement = (node) => {
|
|
11
|
+
const context = node.type === 'html'
|
|
12
|
+
? node.value
|
|
13
|
+
: node.children.every((child) => child.type === 'html')
|
|
14
|
+
? node.children.map((child) => child.value).join('')
|
|
15
|
+
: undefined;
|
|
16
|
+
if (!context) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
const id = getHtmlId(context);
|
|
20
|
+
if (!id) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
return {
|
|
24
|
+
context,
|
|
25
|
+
id,
|
|
26
|
+
node,
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
const getMdxIdAttribute = (node) => {
|
|
30
|
+
const idAttribute = node.attributes.find((attribute) => attribute.type === 'mdxJsxAttribute' &&
|
|
31
|
+
attribute.name === 'id' &&
|
|
32
|
+
typeof attribute.value === 'string');
|
|
33
|
+
return idAttribute;
|
|
34
|
+
};
|
|
35
|
+
const getMdxId = (node) => {
|
|
36
|
+
const idAttribute = getMdxIdAttribute(node);
|
|
37
|
+
return typeof idAttribute?.value === 'string' ? idAttribute.value : undefined;
|
|
38
|
+
};
|
|
39
|
+
const stringifyMdxElement = (node) => {
|
|
40
|
+
const attributes = node.attributes
|
|
41
|
+
.map((attribute) => {
|
|
42
|
+
if (attribute.type === 'mdxJsxExpressionAttribute') {
|
|
43
|
+
return `{...${attribute.value}}`;
|
|
44
|
+
}
|
|
45
|
+
if (attribute.value === null || attribute.value === undefined) {
|
|
46
|
+
return attribute.name;
|
|
47
|
+
}
|
|
48
|
+
if (typeof attribute.value === 'string') {
|
|
49
|
+
return `${attribute.name}="${attribute.value}"`;
|
|
50
|
+
}
|
|
51
|
+
return `${attribute.name}={${attribute.value.value}}`;
|
|
52
|
+
})
|
|
53
|
+
.join(' ');
|
|
54
|
+
const name = node.name ?? '';
|
|
55
|
+
return `<${name}${attributes ? ` ${attributes}` : ''} />`;
|
|
56
|
+
};
|
|
57
|
+
const getIdBearingElement = (node) => {
|
|
58
|
+
if (node.type === 'html' || node.type === 'paragraph') {
|
|
59
|
+
return getHtmlIdBearingElement(node);
|
|
60
|
+
}
|
|
61
|
+
if (node.type === 'mdxJsxFlowElement' || node.type === 'mdxJsxTextElement') {
|
|
62
|
+
const id = getMdxId(node);
|
|
63
|
+
if (!id) {
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
return {
|
|
67
|
+
context: stringifyMdxElement(node),
|
|
68
|
+
id,
|
|
69
|
+
node,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
const getHeadingAnchorExample = (filepath, id) => filepath.endsWith('.mdx') ? `# Title \\{#${id}}` : `# Title {#${id}}`;
|
|
74
|
+
const createMessage = (filepath, element) => `Unexpected id-bearing element \`${element.context}\` associated with a heading, rewrite it to \`${getHeadingAnchorExample(filepath, element.id)}\` instead.`;
|
|
75
|
+
export const headingAnchorFormat = lintRule('doom-lint:heading-anchor-format', (root, vfile) => {
|
|
76
|
+
visitParents(root, ['paragraph', 'html', 'mdxJsxFlowElement', 'mdxJsxTextElement'], (node, parents) => {
|
|
77
|
+
const element = getIdBearingElement(node);
|
|
78
|
+
if (!element) {
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
const report = () => {
|
|
82
|
+
vfile.message(createMessage(vfile.path, element), {
|
|
83
|
+
ancestors: [...parents, node],
|
|
84
|
+
place: element.node.position,
|
|
85
|
+
});
|
|
86
|
+
};
|
|
87
|
+
const parent = parents.at(-1);
|
|
88
|
+
if (parent.type === 'heading') {
|
|
89
|
+
report();
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
const index = parent.children.indexOf(node);
|
|
93
|
+
if (index < 0) {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
for (const siblingIndex of [index - 1, index + 1]) {
|
|
97
|
+
if (siblingIndex < 0 || siblingIndex >= parent.children.length) {
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
const sibling = parent.children[siblingIndex];
|
|
101
|
+
if (sibling.type !== 'heading') {
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
report();
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
});
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import type { Root } from 'mdast';
|
|
2
2
|
import type { Plugin } from 'unified';
|
|
3
3
|
export * from './check-dead-links.ts';
|
|
4
|
+
export * from './file-naming.ts';
|
|
5
|
+
export * from './heading-anchor-format.ts';
|
|
4
6
|
export * from './list-item-punctuation.ts';
|
|
5
7
|
export * from './list-item-size.ts';
|
|
6
8
|
export * from './list-table-introduction.ts';
|
package/lib/remark-lint/index.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import remarkMessageControl from 'remark-message-control';
|
|
2
2
|
export * from "./check-dead-links.js";
|
|
3
|
+
export * from "./file-naming.js";
|
|
4
|
+
export * from "./heading-anchor-format.js";
|
|
3
5
|
export * from "./list-item-punctuation.js";
|
|
4
6
|
export * from "./list-item-size.js";
|
|
5
7
|
export * from "./list-table-introduction.js";
|
package/lib/remarkrc.js
CHANGED
|
@@ -10,7 +10,7 @@ import remarkLintLintMatchPunctuation from 'remark-lint-match-punctuation';
|
|
|
10
10
|
import remarkLintNoChinesePunctuationInNumber from 'remark-lint-no-chinese-punctuation-in-number';
|
|
11
11
|
import remarkLintNoDuplicateHeadingsInSection from 'remark-lint-no-duplicate-headings-in-section';
|
|
12
12
|
import remarkLintNoHiddenTableCell from 'remark-lint-no-hidden-table-cell';
|
|
13
|
-
import doomLint, { checkDeadLinks, noDeepHeading, noDeepList, noMultiOpenAPIPaths, noParagraphIndent, noUnmatchedAnchor, site, tableSize, titleRequired, unitCase, } from "./remark-lint/index.js";
|
|
13
|
+
import doomLint, { checkDeadLinks, headingAnchorFormat, noDeepHeading, noDeepList, noMultiOpenAPIPaths, noParagraphIndent, noUnmatchedAnchor, site, tableSize, titleRequired, unitCase, } from "./remark-lint/index.js";
|
|
14
14
|
export default {
|
|
15
15
|
plugins: [
|
|
16
16
|
remarkDirective,
|
|
@@ -24,6 +24,7 @@ export default {
|
|
|
24
24
|
remarkLintNoHiddenTableCell,
|
|
25
25
|
doomLint,
|
|
26
26
|
checkDeadLinks,
|
|
27
|
+
headingAnchorFormat,
|
|
27
28
|
noDeepHeading,
|
|
28
29
|
noDeepList,
|
|
29
30
|
noMultiOpenAPIPaths,
|