Repository navigation
Expand file tree
/
Copy pathmarkdown.js
More file actions
126 lines (109 loc) · 3.19 KB
/
Copy pathmarkdown.js
File metadata and controls
126 lines (109 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
const remark = require('remark');
const {parseFenceMetadata} = require('./metadata');
/**
* @typedef {Object} MarkdownCodeBlock
* @property {string} code
* @property {number} codeStartLine
* @property {{start: {line: number, column: number}, end: {line: number, column: number}}} position
* @property {{lineIndex: number, rawText: string, metaText: string, range: [number, number]}} fence
* @property {string} filePath
* @property {string} lang
* @property {string} meta
* @property {import('./metadata').FenceMetadata} metadata
*/
const SUPPORTED_LANGUAGES = new Set([
'js',
'jsx',
'javascript',
'ts',
'tsx',
'typescript',
]);
function computeLineOffsets(lines) {
const offsets = [];
let currentOffset = 0;
for (const line of lines) {
offsets.push(currentOffset);
currentOffset += line.length + 1;
}
return offsets;
}
function parseMarkdownFile(content, filePath) {
const tree = remark().parse(content);
const lines = content.split('\n');
const lineOffsets = computeLineOffsets(lines);
const blocks = [];
function traverse(node) {
if (!node || typeof node !== 'object') {
return;
}
if (node.type === 'code') {
const rawLang = node.lang || '';
const normalizedLang = rawLang.toLowerCase();
if (!normalizedLang || !SUPPORTED_LANGUAGES.has(normalizedLang)) {
return;
}
const fenceLineIndex = (node.position?.start?.line ?? 1) - 1;
const fenceStartOffset = node.position?.start?.offset ?? 0;
const fenceLine = lines[fenceLineIndex] ?? '';
const fenceEndOffset = fenceStartOffset + fenceLine.length;
let metaText = '';
if (fenceLine) {
const prefixMatch = fenceLine.match(/^`{3,}\s*/);
const prefixLength = prefixMatch ? prefixMatch[0].length : 3;
metaText = fenceLine.slice(prefixLength + rawLang.length);
} else if (node.meta) {
metaText = ` ${node.meta}`;
}
const metadata = parseFenceMetadata(metaText);
blocks.push({
lang: rawLang || normalizedLang,
meta: node.meta || metaText.trim(),
metadata,
filePath,
code: node.value || '',
codeStartLine: (node.position?.start?.line ?? 1) + 1,
position: {
start: {
line: fenceLineIndex + 1,
column: (node.position?.start?.column ?? 1) - 1,
},
end: {
line: fenceLineIndex + 1,
column: (node.position?.start?.column ?? 1) - 1 + fenceLine.length,
},
},
fence: {
lineIndex: fenceLineIndex,
rawText: fenceLine,
metaText,
range: [fenceStartOffset, fenceEndOffset],
},
});
return;
}
if ('children' in node && Array.isArray(node.children)) {
for (const child of node.children) {
traverse(child);
}
}
}
traverse(tree);
return {
content,
blocks,
lines,
lineOffsets,
};
}
module.exports = {
SUPPORTED_LANGUAGES,
computeLineOffsets,
parseMarkdownFile,
};