|
| 1 | +/** |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +import {describe, expect, it} from 'vitest'; |
| 9 | +import {docuHash} from '@docusaurus/utils'; |
| 10 | +import {fromPartial} from '@total-typescript/shoehorn'; |
| 11 | + |
| 12 | +import {createContentHelpers} from '../contentHelpers'; |
| 13 | +import type {DocMetadata, LoadedContent} from '@docusaurus/plugin-content-docs'; |
| 14 | + |
| 15 | +function createLoadedContent(sources: string[]): LoadedContent { |
| 16 | + return fromPartial<LoadedContent>({ |
| 17 | + loadedVersions: [ |
| 18 | + { |
| 19 | + docs: sources.map((source) => |
| 20 | + fromPartial<DocMetadata>({ |
| 21 | + source, |
| 22 | + }), |
| 23 | + ), |
| 24 | + }, |
| 25 | + ], |
| 26 | + }); |
| 27 | +} |
| 28 | + |
| 29 | +describe('contentHelpers', () => { |
| 30 | + it('resolves MDX loader metadata sources with zero-width spaces', () => { |
| 31 | + const asciiHelpers = createContentHelpers(); |
| 32 | + asciiHelpers.updateContent( |
| 33 | + createLoadedContent([ |
| 34 | + '@site/docs/contribute/vulnerability-report/index.mdx', |
| 35 | + ]), |
| 36 | + ); |
| 37 | + |
| 38 | + expect( |
| 39 | + docuHash( |
| 40 | + asciiHelpers.getMetadataSource( |
| 41 | + '@site/docs/contribute/vulnerability-report/index.mdx', |
| 42 | + ), |
| 43 | + ), |
| 44 | + ).toBe('site-docs-contribute-vulnerability-report-index-mdx-59e'); |
| 45 | + |
| 46 | + const trailingZeroWidthSpaceHelpers = createContentHelpers(); |
| 47 | + trailingZeroWidthSpaceHelpers.updateContent( |
| 48 | + createLoadedContent([ |
| 49 | + '@site/docs/contribute/vulnerability-report\u200B\u200B/index.mdx', |
| 50 | + ]), |
| 51 | + ); |
| 52 | + |
| 53 | + expect( |
| 54 | + docuHash( |
| 55 | + trailingZeroWidthSpaceHelpers.getMetadataSource( |
| 56 | + '@site/docs/contribute/vulnerability-report\u200B/index.mdx', |
| 57 | + ), |
| 58 | + ), |
| 59 | + ).toBe('site-docs-contribute-vulnerability-report-index-mdx-b66'); |
| 60 | + |
| 61 | + const internalZeroWidthSpaceHelpers = createContentHelpers(); |
| 62 | + internalZeroWidthSpaceHelpers.updateContent( |
| 63 | + createLoadedContent([ |
| 64 | + '@site/docs/contribute/vulnerability\u200B-report/index.mdx', |
| 65 | + ]), |
| 66 | + ); |
| 67 | + |
| 68 | + expect( |
| 69 | + docuHash( |
| 70 | + internalZeroWidthSpaceHelpers.getMetadataSource( |
| 71 | + '@site/docs/contribute/vulnerability-report/index.mdx', |
| 72 | + ), |
| 73 | + ), |
| 74 | + ).toBe('site-docs-contribute-vulnerability-report-index-mdx-ed8'); |
| 75 | + |
| 76 | + const unicodeHelpers = createContentHelpers(); |
| 77 | + unicodeHelpers.updateContent( |
| 78 | + createLoadedContent(['@site/docs/contribute/보안-취약점-제보/index.mdx']), |
| 79 | + ); |
| 80 | + |
| 81 | + expect( |
| 82 | + docuHash( |
| 83 | + unicodeHelpers.getMetadataSource( |
| 84 | + '@site/docs/contribute/보안-취약점-제보/index.mdx', |
| 85 | + ), |
| 86 | + ), |
| 87 | + ).toBe('site-docs-contribute-보안-취약점-제보-index-mdx-6b2'); |
| 88 | + }); |
| 89 | + |
| 90 | + it('reports ambiguous zero-width-space metadata sources clearly', () => { |
| 91 | + const helpers = createContentHelpers(); |
| 92 | + |
| 93 | + helpers.updateContent( |
| 94 | + createLoadedContent([ |
| 95 | + '@site/docs/contribute/vulnerability-report/index.mdx', |
| 96 | + '@site/docs/contribute/vulnerability\u200B-report/index.mdx', |
| 97 | + ]), |
| 98 | + ); |
| 99 | + |
| 100 | + expect(() => |
| 101 | + helpers.getMetadataSource( |
| 102 | + '@site/docs/contribute/vulnerability-report/index.mdx', |
| 103 | + ), |
| 104 | + ).toThrow(/U\+200B ZERO WIDTH SPACE/); |
| 105 | + }); |
| 106 | +}); |
0 commit comments