diff --git a/design/author.md b/design/author.md index 9ad905af..1362f22c 100644 --- a/design/author.md +++ b/design/author.md @@ -436,7 +436,9 @@ to 3,000. Nineteen things the code does not say by itself: files and the quiz format build the same package. `readSource` keeps its contract, a source's own problems thrown, without the pack's names. What the build page must do with it is its own: hand the type's file back for a type that is named loosely, which its rule, the source's `type` - as written, does not. + as written, does not. `sourceType(source, names)` is the build's reading of a source's type, + exported for it (#176), so that the page hands over the type the build took and keeps no copy + of the rule: on the 328 stored sources it names what `sourceOf` does in every one. Not built: editing an existing package, which a tool beside it does with `checkContent` as its check; layout for Drag and Drop and Crossword; and any judgement of whether content is right, which diff --git a/packages/author/README.md b/packages/author/README.md index da1e6c04..6fa0f1af 100644 --- a/packages/author/README.md +++ b/packages/author/README.md @@ -180,8 +180,13 @@ refuse it for, empty for a build. `schemaOf(machineName, { markdown, all, libraries })` returns the field list. `writeBundle(type, output, { libraries })` and `writeBundles(folder, { libraries })` write bundles, each `{ output, type, parts, size }`; `bundleLibraries(pack, type)` returns a bundle's parts, and `contentTypes(pack)` the pack's content -types. `ContentGenerator` is the engine underneath, also run by the repository's sweep of every -content type. +types. `sourceType(source, names)` is the content type a source is written in, as a build reads it, +of the machine names given: its `type`, as it is or loosely (`"interactive-book"`), or where it has +none the one name under `contentType` or `library`; `null` where none can be meant, and for a quiz +in the published format (`{ "kind": … }`), which the build reads as a Question Set or a Multiple +Choice by rules of its own. A page that hands a chat a type's notes with a source's problems asks +it, and hands over the type the build took. `ContentGenerator` is the engine underneath, also run by +the repository's sweep of every content type. A tool beside the build takes the libraries as the build loads them, the generator, and the build's rules for text: diff --git a/packages/author/lib/core.mjs b/packages/author/lib/core.mjs index 9045b652..a81a6f53 100644 --- a/packages/author/lib/core.mjs +++ b/packages/author/lib/core.mjs @@ -2,7 +2,7 @@ * @missing-elements/h5p-author/core: what the package does that needs neither Node nor a page, for * a tool that runs beside a build in either: the generator, the libraries loaded as a build loads * them from any source of them, the parts a content type's build can read, the check of saved - * content, and the rules for text. The package's entry for Node (`.`) and the one for a page + * content, the rules for text, and which content type a source is written in, as a build reads it. The package's entry for Node (`.`) and the one for a page * (`./browser`) carry all of it, each with its own `buildPackage` and `openPack`. */ @@ -11,6 +11,7 @@ export { checkContent } from './check.mjs' export { ContentGenerator, librariesIn, libraryString } from './generator.mjs' export { missingForInstall } from './install.mjs' export { loadLibraries, packageLibraries } from './libraries.mjs' +export { sourceType } from './names.mjs' export { GENERATED_END, GENERATED_START } from './schema.mjs' export { SourceError } from './source-error.mjs' export { allowedTags, editorWidget, markupText, plainText, takesMarkup } from './text.mjs' diff --git a/packages/author/lib/names.mjs b/packages/author/lib/names.mjs index d4311655..cabb2cda 100644 --- a/packages/author/lib/names.mjs +++ b/packages/author/lib/names.mjs @@ -97,3 +97,20 @@ export function namingKey(given, names) { } return found.length === 1 ? found[0] : null } + +/** + * The content type a source is written in, as a build reads it (#174, #176): of the machine names + * given, the one its `type` names, as it is or loosely, "interactive-book"; where it has no `type`, + * the one name under `contentType` or `library`. Null where none of them can be meant, and for a + * quiz in the published format, which is read by rules of its own. A page that hands a chat the + * type's notes with a source's problems asks this, so that it hands over the type the build took. + * @param {unknown} source + * @param {Iterable} names machine names + * @returns {string | null} + */ +export function sourceType(source, names) { + if (source === null || typeof source !== 'object' || Array.isArray(source) || 'kind' in source) return null + const given = /** @type {Record} */ (source) + const named = given.type !== undefined ? machineNameOf(given.type, names) : namingKey(given, names) + return named?.machineName ?? null +} diff --git a/packages/author/tests/names.test.ts b/packages/author/tests/names.test.ts index 585d5f91..3dd0f2ab 100644 --- a/packages/author/tests/names.test.ts +++ b/packages/author/tests/names.test.ts @@ -1,5 +1,6 @@ import { describe, expect, it } from 'vitest' -import { machineNameOf, namingKey, nearestNames } from '../lib/names.mjs' +import { sourceOf } from '../lib/build.mjs' +import { machineNameOf, namingKey, nearestNames, sourceType } from '../lib/names.mjs' // The names a chat with no skill beside it wrote for a content type (#174), against a pack's. const NAMES = ['H5P.InteractiveBook', 'H5P.Blanks', 'H5P.AdvancedBlanks', 'H5P.Dialogcards', 'H5P.MultiChoice', 'H5P.QuestionSet', 'H5P.CoursePresentation', 'H5P.Flashcards', 'H5P.Text', 'H5P.AdvancedText', 'H5PEditor.Blanks'] @@ -70,4 +71,33 @@ describe('a library named another way than by its machine name', () => { // Two keys that each name one: neither is taken. expect(namingKey({ type: 'text', contentType: 'Blanks' }, NAMES)).toBeNull() }) + + it('reads the content type a source is written in as the build does, for a page to hand over its notes (#176)', () => { + const cases: [unknown, string | null][] = [ + [{ type: 'H5P.InteractiveBook' }, 'H5P.InteractiveBook'], + [{ type: 'interactive-book', chapters: [] }, 'H5P.InteractiveBook'], + [{ type: ' H5P.Blanks 1.14 ' }, 'H5P.Blanks'], + [{ contentType: 'H5P.InteractiveBook', metadata: { title: 'Physics' } }, 'H5P.InteractiveBook'], + [{ library: 'question set', params: {} }, 'H5P.QuestionSet'], + // `type` is the source's own: under another key a name is read only where there is none. + [{ type: 'interactive-book', contentType: 'H5P.Blanks' }, 'H5P.InteractiveBook'], + [{ type: 'book', contentType: 'H5P.Blanks' }, null], + // Two that name one, a name under another key, a title that reads like one, none at all. + [{ contentType: 'H5P.Blanks', library: 'H5P.Dialogcards' }, null], + [{ mainLibrary: 'H5P.Blanks' }, null], + [{ title: 'Text', params: {} }, null], + [{ type: 'Question Set 2' }, null], + [{}, null], + // A quiz in the published format has rules of its own, and what is no object names nothing. + [{ kind: 'question-set', questions: [] }, null], [{ kind: 'question-set', type: 'H5P.Blanks', questions: [] }, null], + [null, null], [[{ type: 'H5P.Blanks' }], null], ['H5P.Blanks', null] + ] + for (const [source, type] of cases) expect(sourceType(source, NAMES), JSON.stringify(source)).toBe(type) + // What the build takes the source's type to be, the same in every case it reads one. + for (const [source] of cases) { + if (!source || typeof source !== 'object' || Array.isArray(source) || 'kind' in source) continue + const read = sourceOf(source, NAMES) + expect(sourceType(source, NAMES), JSON.stringify(source)).toBe(read.typed ? read.source.type : null) + } + }) })