-
Notifications
You must be signed in to change notification settings - Fork 9
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
4616fb3
commit 5849d4a
Showing
5 changed files
with
82 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { render, screen } from '@testing-library/react'; | ||
import React from 'react'; | ||
import { execute } from '../helpers'; | ||
|
||
describe('variables', () => { | ||
it('renders a variable', async () => { | ||
const md = `{user.name}`; | ||
const Content = await execute(md, {}, { variables: { user: { name: 'Testing' } } }); | ||
|
||
render(<Content />); | ||
|
||
expect(screen.getByText('Testing')).toBeVisible(); | ||
}); | ||
|
||
it('renders a default value', async () => { | ||
const md = `{user.name}`; | ||
const Content = await execute(md); | ||
|
||
render(<Content />); | ||
|
||
expect(screen.getByText('NAME')).toBeVisible(); | ||
}); | ||
|
||
it('supports user variables in ESM', async () => { | ||
const md = ` | ||
export const Hello = () => <p>{user.name}</p>; | ||
<Hello /> | ||
`; | ||
const Content = await execute(md, {}, { variables: { user: { name: 'Owlbert' } } }); | ||
|
||
render(<Content />); | ||
|
||
expect(screen.getByText('Owlbert')).toBeVisible(); | ||
}); | ||
}); |
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,22 +1,16 @@ | ||
import React from 'react'; | ||
import GlossaryContext from './GlossaryTerms'; | ||
import BaseUrlContext from './BaseUrl'; | ||
import { VariablesContext } from '@readme/variable'; | ||
import { RunOpts } from '../lib/run'; | ||
|
||
type Props = React.PropsWithChildren & Pick<RunOpts, 'baseUrl' | 'terms' | 'variables'>; | ||
type Props = React.PropsWithChildren & Pick<RunOpts, 'baseUrl' | 'terms'>; | ||
|
||
const compose = ( | ||
children: React.ReactNode, | ||
...contexts: [React.Context<typeof VariablesContext | typeof GlossaryContext>, unknown][] | ||
) => { | ||
return contexts.reduce((content, [Context, value]) => { | ||
return <Context.Provider value={value}>{content}</Context.Provider>; | ||
}, children); | ||
}; | ||
|
||
const Contexts = ({ children, terms = [], variables = { user: {}, defaults: [] }, baseUrl = '/' }: Props) => { | ||
return compose(children, [GlossaryContext, terms], [VariablesContext, variables], [BaseUrlContext, baseUrl]); | ||
const Contexts = ({ children, terms = [], baseUrl = '/' }: Props) => { | ||
return ( | ||
<GlossaryContext.Provider value={terms}> | ||
<BaseUrlContext.Provider value={baseUrl}>{children}</BaseUrlContext.Provider> | ||
</GlossaryContext.Provider> | ||
); | ||
}; | ||
|
||
export default Contexts; |
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,31 @@ | ||
interface Default { | ||
name: string; | ||
default: string; | ||
} | ||
|
||
export interface Variables { | ||
user: Record<string, string>; | ||
defaults: Default[]; | ||
} | ||
|
||
const User = (variables?: Variables) => { | ||
const { user = {}, defaults = [] } = variables || {}; | ||
|
||
return new Proxy(user, { | ||
get(target, attribute) { | ||
if (typeof attribute === 'symbol') { | ||
return ''; | ||
} | ||
|
||
if (attribute in target) { | ||
return target[attribute]; | ||
} | ||
|
||
const def = defaults.find((d: Default) => d.name === attribute); | ||
|
||
return def ? def.default : attribute.toUpperCase(); | ||
}, | ||
}); | ||
}; | ||
|
||
export default User; |