this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Replace remark-title

+51 -44
-7
src/types.d.ts
··· 1 - declare module 'remark-title' { 2 - interface TitleOptions { 3 - title: string; 4 - } 5 - export default function remarkTitle(opts: TitleOptions): (tree: Root) => undefined; 6 - export type Root = import('mdast').Root; 7 - }
+51 -37
src/unified.ts
··· 1 - import type { List, ListItem, PhrasingContent, Root } from 'mdast'; 1 + import type { Heading, List, ListItem, PhrasingContent, Root } from 'mdast'; 2 2 import { unified } from 'unified'; 3 3 import { visit } from 'unist-util-visit'; 4 4 import { defaultSchema as defaultSanitizeSchema } from 'hast-util-sanitize'; ··· 12 12 import remarkGfm from 'remark-gfm'; 13 13 import remarkParse from 'remark-parse'; 14 14 import remarkSqueezeParagraphs from 'remark-squeeze-paragraphs'; 15 - import remarkTitle from 'remark-title'; 15 + 16 + const toString = (nodes: PhrasingContent[]): string => 17 + nodes.map((node) => { 18 + switch (node.type) { 19 + case 'break': 20 + return '\n'; 21 + case 'delete': 22 + case 'emphasis': 23 + case 'link': 24 + case 'strong': 25 + return toString(node.children); 26 + case 'inlineCode': 27 + return `\`${node.value}\``; 28 + case 'text': 29 + return node.value; 30 + case 'footnoteReference': 31 + case 'html': 32 + case 'image': 33 + case 'imageReference': 34 + case 'linkReference': 35 + default: 36 + return ''; 37 + } 38 + }).join(''); 16 39 17 40 export async function sanitizeHtml(html: string): Promise<string> { 18 41 const vfile = await unified() ··· 161 184 } 162 185 163 186 function extractTitle(markdown: string): string | null { 164 - let depth: number | null = null; 165 - let title: string | null = null; 166 - const toString = (nodes: PhrasingContent[]): string => 167 - nodes.map((node) => { 168 - switch (node.type) { 169 - case 'break': 170 - return '\n'; 171 - case 'delete': 172 - case 'emphasis': 173 - case 'link': 174 - case 'strong': 175 - return toString(node.children); 176 - case 'inlineCode': 177 - return `\`${node.value}\``; 178 - case 'text': 179 - return node.value; 180 - case 'footnoteReference': 181 - case 'html': 182 - case 'image': 183 - case 'imageReference': 184 - case 'linkReference': 185 - default: 186 - return ''; 187 - } 188 - }).join(''); 189 187 const tree = unified() 190 188 .use(remarkParse, { fragment: true }) 191 189 .use(remarkGfm, { ··· 193 191 tableCellPadding: false, 194 192 }) 195 193 .parse(markdown); 196 - visit(tree, function (node) { 197 - if (node.type !== 'heading') 198 - return; 199 - if (!depth || node.depth < depth) 200 - title = toString(node.children); 201 - }); 202 - return title; 194 + const node = tree.children[0]; 195 + if (node && node.type === 'heading' && node.depth === 1) { 196 + return toString(node.children); 197 + } else { 198 + return null; 199 + } 200 + } 201 + 202 + function remarkTitle(opts: { title: string }) { 203 + return function checkTitleTransformer(root: Root) { 204 + const node = root.children[0]!; 205 + const replacement: Heading = { 206 + type: 'heading', 207 + depth: 1, 208 + children: [ 209 + { type: 'text', value: opts.title } 210 + ] 211 + }; 212 + if (node && node.type === 'heading') { 213 + node.depth = 1; 214 + node.children = replacement.children; 215 + } else { 216 + root.children?.unshift(replacement); 217 + } 218 + } 203 219 } 204 220 205 221 export async function transferTitle(from: string, to: string): Promise<string> { ··· 211 227 tablePipeAlign: false, 212 228 tableCellPadding: false, 213 229 }) 214 - .use(remarkTitle, { 215 - title, 216 - }) 230 + .use(remarkTitle, { title }) 217 231 .use(remarkStringify, { 218 232 bullet: '-', 219 233 incrementListMarker: false,