Skip to content

Commit 1bd7ad8

Browse files
committed
Fix #835 handle invalid identifiers in mcdoc renderer
1 parent 1836668 commit 1bd7ad8

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

src/app/components/generator/McdocRenderer.tsx

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,6 @@ const SPECIAL_UNSET = '__unset__'
142142

143143
function StringHead({ type, optional, excludeStrings, node, ctx }: Props<StringType>) {
144144
const { locale } = useLocale()
145-
const { version } = useVersion()
146-
const use1204 = !checkVersion(version, '1.20.5')
147145

148146
const nodeValue = (JsonStringNode.is(node) ? node.value : undefined)?.replaceAll('\n', '\\n')
149147
const [value, setValue] = useState(nodeValue)
@@ -207,11 +205,7 @@ function StringHead({ type, optional, excludeStrings, node, ctx }: Props<StringT
207205
}, [onChangeValue])
208206

209207
return <>
210-
{((idRegistry === 'item' || idRegistry === 'block') && idTags !== 'implicit' && value && !value.startsWith('#')) && <label>
211-
{use1204
212-
? <ItemDisplay1204 item={new ItemStack1204(Identifier1204.parse(value), 1)} />
213-
: <ItemDisplay item={new ItemStack(Identifier.parse(value), 1)} />}
214-
</label>}
208+
{((idRegistry === 'item' || idRegistry === 'block') && idTags !== 'implicit' && value && !value.startsWith('#')) && <ItemIdPreview id={value}/>}
215209
{isSelect ? <>
216210
<select value={value === undefined ? SPECIAL_UNSET : value} onInput={(e) => onChangeValue((e.target as HTMLInputElement).value)}>
217211
{(value === undefined || optional) && <option value={SPECIAL_UNSET}>{locale('unset')}</option>}
@@ -234,6 +228,27 @@ function StringHead({ type, optional, excludeStrings, node, ctx }: Props<StringT
234228
</>
235229
}
236230

231+
function ItemIdPreview({ id }: { id: string }) {
232+
const { version } = useVersion()
233+
234+
const stack = useMemo(() => {
235+
try {
236+
if (!checkVersion(version, '1.20.5')) {
237+
return new ItemStack1204(Identifier1204.parse(id), 1)
238+
}
239+
return new ItemStack(Identifier.parse(id), 1)
240+
} catch (e) {
241+
return undefined
242+
}
243+
}, [id, version])
244+
245+
return <>{stack && <label>
246+
{stack instanceof ItemStack1204
247+
? <ItemDisplay1204 item={stack} />
248+
: <ItemDisplay item={stack} />}
249+
</label>}</>
250+
}
251+
237252
function EnumHead({ type, optional, excludeStrings, node, ctx }: Props<SimplifiedEnum>) {
238253
const { locale } = useLocale()
239254

0 commit comments

Comments
 (0)