Skip to content

Commit

Permalink
format codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
lebalz committed Aug 2, 2024
1 parent c6a40fd commit b35c329
Show file tree
Hide file tree
Showing 57 changed files with 1,244 additions and 1,161 deletions.
17 changes: 17 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
dist
node_modules
.yarn
build
coverage
.docusaurus
.github
.drone.yml

docs
static

*.md
*.json
*.js
*.compose.yml
i18n
17 changes: 9 additions & 8 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"arrowParens": "always",
"bracketSpacing": false,
"bracketSameLine": true,
"printWidth": 80,
"proseWrap": "never",
"singleQuote": true,
"trailingComma": "all"
}
"arrowParens": "always",
"bracketSpacing": true,
"bracketSameLine": false,
"printWidth": 110,
"proseWrap": "never",
"singleQuote": true,
"trailingComma": "none",
"tabWidth": 4
}
78 changes: 40 additions & 38 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@
* LICENSE file in the root directory of this source tree.
*/


/**
* Notes
* - how to add static files: https://github.com/facebook/docusaurus/discussions/6907
* ---> sitemap plugin: https://github.com/facebook/docusaurus/blob/main/packages/docusaurus-plugin-sitemap/src/index.ts
* - call brython with arguments: https://github.com/brython-dev/brython/issues/2421
*
*
*/

import {readDefaultCodeTranslationMessages} from '@docusaurus/theme-translations';
import { readDefaultCodeTranslationMessages } from '@docusaurus/theme-translations';
import type { HtmlTags, LoadContext, Plugin } from '@docusaurus/types';
// eslint-disable-next-line import/no-extraneous-dependencies, import/order
import logger from '@docusaurus/logger';
Expand All @@ -39,15 +38,12 @@ const extractImports = (script: string): string[] => {
return imports.push(importImport[1]);
}
});
return imports
}
return imports;
};

const theme: Plugin<{ remoteHeadTags: HtmlTags[] }> = (
context: LoadContext,
options: ThemeOptions,
) => {
const theme: Plugin<{ remoteHeadTags: HtmlTags[] }> = (context: LoadContext, options: ThemeOptions) => {
const {
i18n: {currentLocale},
i18n: { currentLocale }
} = context;
const libDir = options.libDir || DEFAULT_LIB_DIR;
const isRemote = /https?:\/\//.test(libDir);
Expand All @@ -57,7 +53,7 @@ const theme: Plugin<{ remoteHeadTags: HtmlTags[] }> = (
async loadContent() {
const staticDir = path.join(context.siteDir, context.siteConfig.staticDirectories[0], libDir);
let assets: string[] = [];
const bryModules: {name: string, content: string}[] = [];
const bryModules: { name: string; content: string }[] = [];
if (!options.skipCopyAssetsToLibDir && !isRemote) {
await fs.ensureDir(staticDir);
if (process.env.NODE_ENV !== 'production') {
Expand All @@ -72,7 +68,7 @@ const theme: Plugin<{ remoteHeadTags: HtmlTags[] }> = (
}
if (isHashRouter && !isRemote) {
const libs = await fs.readdir(staticDir);
const libraries: {[key: string]: string} = {};
const libraries: { [key: string]: string } = {};
for (const lib of libs) {
const libPath = path.join(staticDir, lib);
if (libPath.endsWith('.py')) {
Expand All @@ -84,32 +80,38 @@ const theme: Plugin<{ remoteHeadTags: HtmlTags[] }> = (
for (const libName of libNames) {
const libContent = libraries[libName];
const imports = extractImports(libContent);
const injectAt = Math.max(...bryModules.map((lib, idx) => imports.includes(lib.name) ? idx : -1), -1);
const injectAt = Math.max(
...bryModules.map((lib, idx) => (imports.includes(lib.name) ? idx : -1)),
-1
);
if (injectAt < 0) {
bryModules.splice(0, 0, {name: libName, content: libContent});
bryModules.splice(0, 0, { name: libName, content: libContent });
} else if (injectAt + 1 < bryModules.length) {
bryModules.splice(injectAt + 1, 0, {name: libName, content: libContent});
bryModules.splice(injectAt + 1, 0, {
name: libName,
content: libContent
});
} else {
bryModules.push({name: libName, content: libContent});
bryModules.push({ name: libName, content: libContent });
}
}
}
return {
assets: assets,
bryModules: bryModules
}
};
},
getDefaultCodeTranslationMessages() {
return readDefaultCodeTranslationMessages({
locale: currentLocale,
name: NAME,
});
return readDefaultCodeTranslationMessages({
locale: currentLocale,
name: NAME
});
},
async contentLoaded({ content, actions }) {
const { setGlobalData } = actions;
const libUrl = isRemote ? libDir : path.join(context.baseUrl, libDir, '/');
setGlobalData({
libDir: libUrl,
setGlobalData({
libDir: libUrl,
syncMaxOnceEvery: options.syncMaxOnceEvery || DEFAULT_OPTIONS.syncMaxOnceEvery
});
},
Expand All @@ -120,9 +122,9 @@ const theme: Plugin<{ remoteHeadTags: HtmlTags[] }> = (
{
test: /\.raw\.*/,
type: 'asset/source'
},
],
},
}
]
}
};
},
getThemePath() {
Expand All @@ -131,27 +133,27 @@ const theme: Plugin<{ remoteHeadTags: HtmlTags[] }> = (
getTypeScriptThemePath() {
return '../src/theme';
},
injectHtmlTags({content}: {content: {bryModules: {name: string, content: string}[]}}) {
injectHtmlTags({ content }: { content: { bryModules: { name: string; content: string }[] } }) {
return {
headTags: [
{
tagName: 'script',
attributes: {
src: options.brythonSrc || DEFAULT_OPTIONS.brythonSrc,
crossorigin: "anonymous",
referrerpolicy: "no-referrer",
crossorigin: 'anonymous',
referrerpolicy: 'no-referrer',
defer: 'defer'
},
}
},
{
tagName: 'script',
attributes: {
src: options.brythonStdlibSrc || DEFAULT_OPTIONS.brythonStdlibSrc,
crossorigin: "anonymous",
referrerpolicy: "no-referrer",
crossorigin: 'anonymous',
referrerpolicy: 'no-referrer',
defer: 'defer'
},
},
}
}
],
postBodyTags: content.bryModules.map((module) => {
return {
Expand All @@ -161,16 +163,16 @@ const theme: Plugin<{ remoteHeadTags: HtmlTags[] }> = (
type: 'text/python'
},
innerHTML: module.content
}
};
})
};
},
getSwizzleComponentList() {
return [];
}
} as Plugin;
}
};

export default theme;
export {validateThemeConfig} from './options';
export {ThemeOptions, Options};
export { validateThemeConfig } from './options';
export { ThemeOptions, Options };
20 changes: 10 additions & 10 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
* LICENSE file in the root directory of this source tree.
*/

import {Joi} from '@docusaurus/utils-validation';
import { Joi } from '@docusaurus/utils-validation';

import type {ThemeConfigValidationContext} from '@docusaurus/types';
import type { ThemeConfigValidationContext } from '@docusaurus/types';

export type ThemeOptions = {
/**
Expand Down Expand Up @@ -42,8 +42,7 @@ export type ThemeOptions = {
* @default 1000
*/
syncMaxOnceEvery: number;
}

};

export type Options = Partial<ThemeOptions>;

Expand All @@ -63,9 +62,10 @@ const ThemeOptionSchema = Joi.object<ThemeOptions>({
syncMaxOnceEvery: Joi.number().default(DEFAULT_OPTIONS.syncMaxOnceEvery)
});

export function validateThemeConfig(
{themeConfig, validate}: ThemeConfigValidationContext<Options, ThemeOptions>
): ThemeOptions {
const validatedConfig = validate(ThemeOptionSchema, themeConfig);
return validatedConfig;
}
export function validateThemeConfig({
themeConfig,
validate
}: ThemeConfigValidationContext<Options, ThemeOptions>): ThemeOptions {
const validatedConfig = validate(ThemeOptionSchema, themeConfig);
return validatedConfig;
}
60 changes: 32 additions & 28 deletions src/theme/CodeBlock/index.tsx
Original file line number Diff line number Diff line change
@@ -1,39 +1,49 @@
import React from 'react';
import CodeBlock, {type Props as CodeBlockType} from '@theme-init/CodeBlock';
import CodeBlock, { type Props as CodeBlockType } from '@theme-init/CodeBlock';
import type { WrapperProps } from '@docusaurus/types';
import ExecutionEnvironment from '@docusaurus/ExecutionEnvironment';
import CodeEditor, { type MetaProps } from '@theme/CodeEditor';

import ScriptContext from '@theme/CodeEditor/WithScript/ScriptContext';
import ContextEditor from '@theme/CodeEditor/ContextEditor';


type Props = WrapperProps<typeof CodeBlockType>;


const sanitizedTitle = (id: string) => {
if (!id) {
return;
}
return id.replace(/--/g, '<<HYPHEN>>').replace(/__/g, '<<UNDERSCORE>>').replace(/[-_]/g, ' ').replace(/<<UNDERSCORE>>/g, '_').replace(/<<HYPHEN>>/g, '-')
}
return id
.replace(/--/g, '<<HYPHEN>>')
.replace(/__/g, '<<UNDERSCORE>>')
.replace(/[-_]/g, ' ')
.replace(/<<UNDERSCORE>>/g, '_')
.replace(/<<HYPHEN>>/g, '-');
};

const extractMetaProps = (props: {metastring?: string}): MetaProps => {
const extractMetaProps = (props: { metastring?: string }): MetaProps => {
const metaString = (props?.metastring || '').replace(/\s*=\s*/g, '='); // remove spaces around =
const metaRaw = metaString.split(/\s+/).map((s) => s.trim().split('='));
return metaRaw.reduce((acc, [key, value]) => {
if (!key) {
return metaRaw.reduce(
(acc, [key, value]) => {
if (!key) {
return acc;
}
/** casts to booleans and numbers. When no value was provided, true is used */
const val =
value === 'true'
? true
: value === 'false'
? false
: !Number.isNaN(Number(value))
? Number(value)
: value || true;
acc[key] = val;
return acc;
}
/** casts to booleans and numbers. When no value was provided, true is used */
const val = value === 'true' ? true
: value === 'false' ? false
: !Number.isNaN(Number(value)) ? Number(value)
: value || true;
acc[key] = val;
return acc;
}, {} as {[key: string]: number | string | boolean});
}
},
{} as { [key: string]: number | string | boolean }
);
};

export default function CodeBlockWrapper(props: Props): JSX.Element {
const metaProps = extractMetaProps(props);
Expand All @@ -48,19 +58,13 @@ export default function CodeBlockWrapper(props: Props): JSX.Element {
if (metaProps.live_py && ExecutionEnvironment.canUseDOM) {
const title = props.title || metaProps.title;

const rawcode: string = (props.children as string || '').replace(/\s*\n$/, '');
const rawcode: string = ((props.children as string) || '').replace(/\s*\n$/, '');
let code = rawcode;
return (
<ContextEditor
{...props}
{...metaProps}
title={sanitizedTitle(title) || lang}
>
<ContextEditor {...props} {...metaProps} title={sanitizedTitle(title) || lang}>
{props.children}
</ContextEditor>
);
}
return (
<CodeBlock {...props} />
);
}
return <CodeBlock {...props} />;
}
Loading

0 comments on commit b35c329

Please sign in to comment.