-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bd4d1ae
commit 33a4272
Showing
5 changed files
with
123 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import type { Html } from 'mdast'; | ||
import { SKIP } from 'unist-util-visit'; | ||
import { z } from 'zod'; | ||
|
||
import type { MacroFunction, MacroNode } from '../types.ts'; | ||
import { wrappedStringSchema } from '../validation.ts'; | ||
|
||
function parseArguments( | ||
value: string[], | ||
): [string, string | undefined, string, boolean] { | ||
if (value.length === 0 || !value[0]) { | ||
throw new Error('No arguments provided'); | ||
} | ||
return [ | ||
wrappedStringSchema.parse(value[0]), | ||
value[1] ? wrappedStringSchema.parse(value[1]) : undefined, | ||
value[2] ? wrappedStringSchema.parse(value[2]) : '', | ||
value[3] | ||
? z | ||
.number() | ||
.transform((value_) => !!value_) | ||
.parse(JSON.parse(value[3])) | ||
: false, | ||
]; | ||
} | ||
|
||
function macro(node: MacroNode): Html { | ||
const targetLocale = process.env.TARGET_LOCALE; | ||
const arguments_ = parseArguments(node.parameters); | ||
let header = arguments_[0]; | ||
let string_ = arguments_[1] || arguments_[0]; | ||
let URL = '/' + targetLocale + '/docs/Web/HTTP/Headers/' + header; | ||
let anch = ''; | ||
|
||
if (arguments_[2]) { | ||
string_ = string_ + '.' + arguments_[2]; | ||
anch = '#' + arguments_[2]; | ||
} | ||
|
||
let code = ''; | ||
let endcode = ''; | ||
if (!arguments_[3]) { | ||
code = '<code>'; | ||
endcode = '</code>'; | ||
} | ||
|
||
return { | ||
type: 'html', | ||
value: `<a href="${URL + anch}">${code}${string_}${endcode}</a>`, | ||
}; | ||
} | ||
|
||
const HTTPHeader: MacroFunction = (node, index, parent) => { | ||
const replacement = macro(node); | ||
parent.children[index] = replacement; | ||
return [SKIP, index]; | ||
}; | ||
|
||
export default HTTPHeader; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import type { Html } from 'mdast'; | ||
import { SKIP } from 'unist-util-visit'; | ||
import { z } from 'zod'; | ||
|
||
import type { MacroFunction, MacroNode } from '../types.ts'; | ||
import { wrappedStringSchema } from '../validation.ts'; | ||
|
||
function parseArguments( | ||
value: string[], | ||
): [string, string | undefined, string, boolean] { | ||
if (value.length === 0 || !value[0]) { | ||
throw new Error('No arguments provided'); | ||
} | ||
return [ | ||
wrappedStringSchema.parse(value[0]), | ||
value[1] ? wrappedStringSchema.parse(value[1]) : undefined, | ||
value[2] ? wrappedStringSchema.parse(value[2]) : '', | ||
value[3] | ||
? z | ||
.number() | ||
.transform((value_) => !!value_) | ||
.parse(JSON.parse(value[3])) | ||
: false, | ||
]; | ||
} | ||
|
||
function macro(node: MacroNode): Html { | ||
const targetLocale = process.env.TARGET_LOCALE; | ||
const arguments_ = parseArguments(node.parameters); | ||
let method = arguments_[0].toUpperCase(); | ||
let string_ = arguments_[1] || arguments_[0]; | ||
let URL = '/' + targetLocale + '/docs/Web/HTTP/Methods/' + method; | ||
let anch = ''; | ||
|
||
if (arguments_[2]) { | ||
string_ = string_ + '.' + arguments_[2]; | ||
anch = '#' + arguments_[2]; | ||
} | ||
|
||
let code = ''; | ||
let endcode = ''; | ||
if (!arguments_[3]) { | ||
code = '<code>'; | ||
endcode = '</code>'; | ||
} | ||
|
||
return { | ||
type: 'html', | ||
value: `<a href="${URL + anch}">${code}${string_}${endcode}</a>`, | ||
}; | ||
} | ||
|
||
const HTTPMethod: MacroFunction = (node, index, parent) => { | ||
const replacement = macro(node); | ||
parent.children[index] = replacement; | ||
return [SKIP, index]; | ||
}; | ||
|
||
export default HTTPMethod; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters