Skip to content

Commit

Permalink
httpheader & httpmethod
Browse files Browse the repository at this point in the history
  • Loading branch information
viperehonchuk committed Feb 20, 2024
1 parent bd4d1ae commit 33a4272
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 1 deletion.
1 change: 0 additions & 1 deletion src/plugins/rehype/inject-link-classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const EXTERNAL_LINK_CLASS = 'wd-external';
const MISSING_LINK_CLASS = 'wd-nav-link-not-translated';

function getClassesByUrl(url: string): string[] {
console.log('getClassesByUrl', url);
if (url.startsWith('#')) {
return [];
}
Expand Down
1 change: 1 addition & 0 deletions src/plugins/remark/macros/expand.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ describe('expandMacros', () => {
vi.resetModules(); // Most important - it clears the cache
process.env = {
PATH_TO_LOCALIZED_CONTENT: 'path/to/localized/content',
PATH_TO_ORIGINAL_CONTENT: 'path/to/original/content',
TARGET_LOCALE: 'uk',
};
});
Expand Down
59 changes: 59 additions & 0 deletions src/plugins/remark/macros/macros/HTTPHeader.ts
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;
59 changes: 59 additions & 0 deletions src/plugins/remark/macros/macros/HTTPMethod.ts
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;
4 changes: 4 additions & 0 deletions src/plugins/remark/macros/macros/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import EmbedLiveSample from './EmbedLiveSample/index.ts';
import GlossaryDisambiguation from './GlossaryDisambiguation.ts';
import GlossarySidebar from './GlossarySidebar.ts';
import HTMLElement from './HTMLElement.ts';
import HTTPHeader from './HTTPHeader.ts';
import HTTPMethod from './HTTPMethod.ts';
import cssxref from './cssxref.ts';
import domxref from './domxref.ts';
import glossary from './glossary.ts';
Expand All @@ -15,6 +17,8 @@ const MACROS: Record<string, MacroFunction> = {
GlossaryDisambiguation,
GlossarySidebar,
HTMLElement,
HTTPHeader,
HTTPMethod,
cssxref,
domxref,
glossary,
Expand Down

0 comments on commit 33a4272

Please sign in to comment.