Skip to content

Commit 97173c4

Browse files
authored
Convert unified/processor.js to TypeScript (Phase 1 - Core Processor) (#56426)
1 parent dbbc1e2 commit 97173c4

File tree

3 files changed

+98
-10
lines changed

3 files changed

+98
-10
lines changed

src/content-render/types.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* Types for the content-render module
3+
*/
4+
5+
/**
6+
* Context interface for content rendering operations
7+
*/
8+
export interface Context {
9+
currentLanguage?: string
10+
autotitleLanguage?: string
11+
currentVersion?: string
12+
currentProduct?: string
13+
markdownRequested?: boolean
14+
pages?: any
15+
redirects?: any
16+
page?: {
17+
fullPath: string
18+
[key: string]: any
19+
}
20+
[key: string]: any
21+
}
22+
23+
/**
24+
* Options for rendering operations
25+
*/
26+
export interface RenderOptions {
27+
cache?: boolean | ((template: string, context: Context) => string | null)
28+
filename?: string
29+
textOnly?: boolean
30+
[key: string]: any
31+
}
32+
33+
/**
34+
* Unified processor plugin function type
35+
*/
36+
export type UnifiedPlugin = (context?: Context) => any
37+
38+
/**
39+
* VFile interface for unified processing
40+
*/
41+
export interface VFile {
42+
toString(): string
43+
[key: string]: any
44+
}
45+
46+
/**
47+
* Unified processor interface
48+
*/
49+
export interface UnifiedProcessor {
50+
process(content: string): Promise<VFile>
51+
use(plugin: any, ...args: any[]): UnifiedProcessor
52+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Type declarations for modules without TypeScript definitions
3+
*/
4+
5+
declare module 'remark-gemoji-to-emoji' {
6+
import type { Plugin } from 'unified'
7+
const plugin: Plugin
8+
export default plugin
9+
}
10+
11+
declare module 'remark-remove-comments' {
12+
import type { Plugin } from 'unified'
13+
const plugin: Plugin
14+
export default plugin
15+
}
16+
17+
declare module 'rehype-highlight' {
18+
import type { Plugin } from 'unified'
19+
import type { Options } from 'lowlight'
20+
21+
interface HighlightOptions extends Options {
22+
languages?: Record<string, any>
23+
subset?: boolean
24+
aliases?: Record<string, string>
25+
}
26+
27+
const plugin: Plugin<[HighlightOptions?]>
28+
export default plugin
29+
}

src/content-render/unified/processor.js renamed to src/content-render/unified/processor.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ import annotate from './annotate'
2929
import alerts from './alerts'
3030
import removeHtmlComments from 'remark-remove-comments'
3131
import remarkStringify from 'remark-stringify'
32+
import type { Context, UnifiedProcessor } from '@/content-render/types'
3233

33-
export function createProcessor(context) {
34+
export function createProcessor(context: Context): UnifiedProcessor {
3435
return (
3536
unified()
3637
.use(remarkParse)
@@ -44,11 +45,13 @@ export function createProcessor(context) {
4445
.use(remark2rehype, { allowDangerousHtml: true })
4546
// HTML AST below vvv
4647
.use(slug)
47-
.use(useEnglishHeadings, context)
48+
// useEnglishHeadings plugin requires context with englishHeadings property
49+
.use(useEnglishHeadings as any, context || {})
4850
.use(headingLinks)
4951
.use(codeHeader)
5052
.use(annotate, context)
51-
.use(highlight, {
53+
// Using 'as any' for highlight plugin due to complex type mismatch between unified and rehype-highlight
54+
.use(highlight as any, {
5255
languages: { ...common, graphql, dockerfile, http, groovy, erb, powershell },
5356
subset: false,
5457
aliases: {
@@ -71,24 +74,28 @@ export function createProcessor(context) {
7174
.use(rewriteForRowheaders)
7275
.use(rewriteImgSources)
7376
.use(rewriteAssetImgTags)
74-
.use(alerts, context)
77+
// alerts plugin requires context with alertTitles property
78+
.use(alerts as any, context || {})
7579
// HTML AST above ^^^
76-
.use(html)
77-
// String below vvv
80+
.use(html) as UnifiedProcessor // String below vvv
7881
)
7982
}
8083

81-
export function createMarkdownOnlyProcessor(context) {
82-
return unified().use(remarkParse).use(gfm).use(rewriteLocalLinks, context).use(remarkStringify)
84+
export function createMarkdownOnlyProcessor(context: Context): UnifiedProcessor {
85+
return unified()
86+
.use(remarkParse)
87+
.use(gfm)
88+
.use(rewriteLocalLinks, context)
89+
.use(remarkStringify) as UnifiedProcessor
8390
}
8491

85-
export function createMinimalProcessor(context) {
92+
export function createMinimalProcessor(context: Context): UnifiedProcessor {
8693
return unified()
8794
.use(remarkParse)
8895
.use(gfm)
8996
.use(rewriteLocalLinks, context)
9097
.use(remark2rehype, { allowDangerousHtml: true })
9198
.use(slug)
9299
.use(raw)
93-
.use(html)
100+
.use(html) as UnifiedProcessor
94101
}

0 commit comments

Comments
 (0)