-
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
2b05b1d
commit 1fd5669
Showing
14 changed files
with
169 additions
and
44 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
import registry from './registry.ts'; | ||
import { translatedContentRegistry } from './index.ts'; | ||
|
||
export default function getAll() { | ||
if (registry.size === 0) { | ||
if (translatedContentRegistry.size === 0) { | ||
throw new Error('Registry is not initialized'); | ||
} | ||
return [...registry.values()]; | ||
return [...translatedContentRegistry.values()]; | ||
} |
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 |
---|---|---|
@@ -1,11 +1,11 @@ | ||
import registry from './registry.js'; | ||
import { translatedContentRegistry } from './index.ts'; | ||
|
||
export default function hasPage(slug: string): boolean { | ||
if (!slug) { | ||
throw new Error('Slug is required'); | ||
} | ||
if (registry.size === 0) { | ||
if (translatedContentRegistry.size === 0) { | ||
throw new Error('Registry is not initialized'); | ||
} | ||
return registry.has(slug); | ||
return translatedContentRegistry.has(slug); | ||
} |
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,4 @@ | ||
import type { RawPage } from './validation.ts'; | ||
|
||
export const translatedContentRegistry = new Map<string, RawPage>(); | ||
export const originalContentRegistry = new Map<string, RawPage>(); |
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,14 @@ | ||
import { originalContentRegistry } from './index.ts'; | ||
import { initRegistry } from './registry.ts'; | ||
|
||
export default async function initOriginalRegistry() { | ||
const PATH_TO_ORIGINAL_CONTENT = process.env.PATH_TO_ORIGINAL_CONTENT; | ||
if (!PATH_TO_ORIGINAL_CONTENT) { | ||
throw new Error('process.env.PATH_TO_ORIGINAL_CONTENT is not defined'); | ||
} | ||
await initRegistry( | ||
originalContentRegistry, | ||
PATH_TO_ORIGINAL_CONTENT, | ||
'en-us', | ||
); | ||
} |
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,18 @@ | ||
import { translatedContentRegistry } from './index.ts'; | ||
import { initRegistry } from './registry.ts'; | ||
|
||
export default async function initTranslatedRegistry() { | ||
const PATH_TO_LOCALIZED_CONTENT = process.env.PATH_TO_LOCALIZED_CONTENT; | ||
if (!PATH_TO_LOCALIZED_CONTENT) { | ||
throw new Error('process.env.PATH_TO_LOCALIZED_CONTENT is not defined'); | ||
} | ||
const TARGET_LOCALE = process.env.TARGET_LOCALE; | ||
if (!TARGET_LOCALE) { | ||
throw new Error('process.env.TARGET_LOCALE is not defined'); | ||
} | ||
await initRegistry( | ||
translatedContentRegistry, | ||
PATH_TO_LOCALIZED_CONTENT, | ||
TARGET_LOCALE, | ||
); | ||
} |
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
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,91 @@ | ||
import type { Html } from 'mdast'; | ||
import { SKIP } from 'unist-util-visit'; | ||
|
||
import { | ||
originalContentRegistry, | ||
translatedContentRegistry, | ||
} from '../../../registry/index.ts'; | ||
import type { MacroFunction, MacroNode } from '../types.ts'; | ||
import { wrappedStringSchema } from '../validation.ts'; | ||
|
||
function parseArguments(value: string[]): [string, string | undefined, string] { | ||
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]) : '', | ||
]; | ||
} | ||
|
||
function macro(node: MacroNode): Html { | ||
const targetLocale = process.env.TARGET_LOCALE; | ||
let arguments_ = parseArguments(node.parameters); | ||
let url = ''; | ||
let urlWithoutAnchor = ''; | ||
let displayName = arguments_[1] || arguments_[0]; | ||
|
||
// Deal with CSS data types by removing <> | ||
let slug = arguments_[0].replaceAll(/<(.*)>/g, '$1'); | ||
|
||
// Special case <color>, <flex>, and <position> | ||
switch (arguments_[0]) { | ||
case '<color>': { | ||
slug = 'color_value'; | ||
break; | ||
} | ||
|
||
case '<flex>': { | ||
slug = 'flex_value'; | ||
break; | ||
} | ||
|
||
case '<position>': { | ||
slug = 'position_value'; | ||
break; | ||
} | ||
} | ||
|
||
const basePath = `/${targetLocale}/docs/Web/CSS/`; | ||
urlWithoutAnchor = basePath + slug; | ||
url = urlWithoutAnchor + arguments_[2]; | ||
|
||
const thisPage = | ||
translatedContentRegistry.get(`Web/CSS/${slug}`) || | ||
originalContentRegistry.get(`Web/CSS/${slug}`); | ||
|
||
if (!thisPage) { | ||
throw new Error(`No page found for ${slug}`); | ||
} | ||
|
||
if (!arguments_[1]) { | ||
// Append parameter brackets to CSS functions | ||
if ( | ||
thisPage['page-type'] === 'css-function' && | ||
!displayName.endsWith('()') | ||
) { | ||
displayName += '()'; | ||
} | ||
// Enclose CSS data types in arrow brackets | ||
if ( | ||
thisPage['page-type'] === 'css-type' && | ||
!/^<.+>$/.test(displayName) | ||
) { | ||
displayName = '<' + displayName + '>'; | ||
} | ||
} | ||
|
||
return { | ||
type: 'html', | ||
value: `<a href="${url}" title="${thisPage?.title}"><code>${displayName}</code></a>`, | ||
}; | ||
} | ||
|
||
const cssxref: MacroFunction = (node, index, parent) => { | ||
const replacement = macro(node); | ||
parent.children[index] = replacement; | ||
return [SKIP, index]; | ||
}; | ||
|
||
export default cssxref; |
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