Translation Metadata addition to GLaaS#189
Conversation
99afb62 to
300c93d
Compare
| const { id, selector } = processSchemaKey(key); | ||
| const fields = []; | ||
| blockData.data.forEach((field) => { | ||
| const fieldName = field['field name']; |
There was a problem hiding this comment.
No idea how rigid schemaData is, but is there any need to possibly support field_name too?
There was a problem hiding this comment.
It is dev/super-author file. So, I don't think we need to support field_name. Originally I had fieldName in the sheet, but Chris M. felt it was too code-like :)
chrischrischris
left a comment
There was a problem hiding this comment.
A few optimizations to reduce querySelector usage. Might be overkill if the docs are small
|
|
||
| const BLOCK_SCHEMA_PATH = '/.da/block-schema.json'; | ||
|
|
||
| let BLOCK_SCHEMA_CACHE; |
There was a problem hiding this comment.
nit: All caps usually used for constants
| function unwrapSoleParagraphs(doc) { | ||
| doc.querySelectorAll('div[class]').forEach((block) => { | ||
| const rows = block.querySelectorAll(':scope > div'); | ||
| rows.forEach((row) => { | ||
| const columns = row.querySelectorAll(':scope > div'); | ||
| columns.forEach((div) => { | ||
| if (div.children.length === 1 && div.children[0].tagName === 'P') { | ||
| const pTag = div.children[0]; | ||
| div.replaceChildren(...pTag.childNodes); | ||
| } | ||
| }); | ||
| }); | ||
| }); | ||
| } |
There was a problem hiding this comment.
Can be optimized a bit
| function unwrapSoleParagraphs(doc) { | |
| doc.querySelectorAll('div[class]').forEach((block) => { | |
| const rows = block.querySelectorAll(':scope > div'); | |
| rows.forEach((row) => { | |
| const columns = row.querySelectorAll(':scope > div'); | |
| columns.forEach((div) => { | |
| if (div.children.length === 1 && div.children[0].tagName === 'P') { | |
| const pTag = div.children[0]; | |
| div.replaceChildren(...pTag.childNodes); | |
| } | |
| }); | |
| }); | |
| }); | |
| } | |
| function unwrapSoleParagraphs(doc) { | |
| doc.querySelectorAll('div[class] > div > div').forEach((div) => { | |
| if (div.children.length === 1 && div.children[0].tagName === 'P') { | |
| const pTag = div.children[0]; | |
| div.replaceChildren(...pTag.childNodes); | |
| } | |
| }); | |
| } |
| Object.values(parsedSchema).forEach((block) => { | ||
| const { selector, fields } = block; | ||
| const blockElements = doc.querySelectorAll(selector); | ||
|
|
||
| blockElements.forEach((blockElement, blockIndex) => { | ||
| const blockId = Object.keys(parsedSchema).find( | ||
| (id) => parsedSchema[id].selector === selector, | ||
| ); | ||
| const rows = blockElement.querySelectorAll(':scope > div'); | ||
| rows.forEach((row) => { | ||
| const labelDiv = row.querySelector(':scope > div:nth-child(1)'); | ||
| const contentDiv = row.querySelector(':scope > div:nth-child(2)'); | ||
| if (!labelDiv || !contentDiv) return; | ||
| const field = fields.find((f) => isExactMatch(labelDiv, f.fieldName)); | ||
| if (!field) return; | ||
| const { fieldName, fieldKey, charCount, keywordsInjection } = field; | ||
| if (charCount) { | ||
| contentDiv.setAttribute('its-storage-size', charCount); | ||
| } | ||
| const keywordsValue = String(keywordsInjection); | ||
| const locNoteValue = `block-name=${blockId}_${blockIndex + 1}_${fieldKey}|fieldName=${fieldName}|apply-keywords=${keywordsValue}`; | ||
| contentDiv.setAttribute('its-loc-note', locNoteValue); | ||
| contentDiv.setAttribute('its-loc-note-type', 'description'); | ||
| }); | ||
| }); | ||
| }); |
There was a problem hiding this comment.
can be optimized a bit
| Object.values(parsedSchema).forEach((block) => { | |
| const { selector, fields } = block; | |
| const blockElements = doc.querySelectorAll(selector); | |
| blockElements.forEach((blockElement, blockIndex) => { | |
| const blockId = Object.keys(parsedSchema).find( | |
| (id) => parsedSchema[id].selector === selector, | |
| ); | |
| const rows = blockElement.querySelectorAll(':scope > div'); | |
| rows.forEach((row) => { | |
| const labelDiv = row.querySelector(':scope > div:nth-child(1)'); | |
| const contentDiv = row.querySelector(':scope > div:nth-child(2)'); | |
| if (!labelDiv || !contentDiv) return; | |
| const field = fields.find((f) => isExactMatch(labelDiv, f.fieldName)); | |
| if (!field) return; | |
| const { fieldName, fieldKey, charCount, keywordsInjection } = field; | |
| if (charCount) { | |
| contentDiv.setAttribute('its-storage-size', charCount); | |
| } | |
| const keywordsValue = String(keywordsInjection); | |
| const locNoteValue = `block-name=${blockId}_${blockIndex + 1}_${fieldKey}|fieldName=${fieldName}|apply-keywords=${keywordsValue}`; | |
| contentDiv.setAttribute('its-loc-note', locNoteValue); | |
| contentDiv.setAttribute('its-loc-note-type', 'description'); | |
| }); | |
| }); | |
| }); | |
| Object.entries(parsedSchema).forEach(([blockId, block]) => { | |
| const { selector, fields } = block; | |
| const blockElements = doc.querySelectorAll(selector); | |
| blockElements.forEach((blockElement, blockIndex) => { | |
| const rows = blockElement.querySelectorAll(':scope > div'); | |
| rows.forEach((row) => { | |
| const labelDiv = row.children[0]; | |
| const contentDiv = row.children[1]; | |
| if (!labelDiv || !contentDiv || labelDiv.tagName !== 'DIV' || contentDiv.tagName !== 'DIV') { | |
| return; | |
| } | |
| const field = fields.find((f) => isExactMatch(labelDiv, f.fieldName)); | |
| if (!field) return; | |
| const { fieldName, fieldKey, charCount, keywordsInjection } = field; | |
| if (charCount) { | |
| contentDiv.setAttribute('its-storage-size', charCount); | |
| } | |
| const keywordsValue = String(keywordsInjection); | |
| const locNoteValue = `block-name=${blockId}_${blockIndex + 1}_${fieldKey}|fieldName=${fieldName}|apply-keywords=${keywordsValue}`; | |
| contentDiv.setAttribute('its-loc-note', locNoteValue); | |
| contentDiv.setAttribute('its-loc-note-type', 'description'); | |
| }); | |
| }); | |
| }); |
| Object.keys(keywordsData).forEach((key) => { | ||
| if (key.startsWith(':')) return; | ||
| const blockData = keywordsData[key]; | ||
| if (!blockData.data) return; |
There was a problem hiding this comment.
nit: use Object.entries to get both vals at once
| Object.keys(keywordsData).forEach((key) => { | |
| if (key.startsWith(':')) return; | |
| const blockData = keywordsData[key]; | |
| if (!blockData.data) return; | |
| Object.entries(keywordsData).forEach(([key, blockData]) => { | |
| if (key.startsWith(':') || !blockData?.data) continue; |
| blockData.data.forEach((entry) => { | ||
| const languageName = entry.language; | ||
| if (!languageName) return; | ||
| const langCode = languageNameToCode(languageName, langs); |
There was a problem hiding this comment.
For the languageNameToCode and fieldNameToKey calls, if they're being called a lot for the same values you could memoize the lookups. Not sure how large keywordsData/blockData can get to
| if (hasKeywords) { | ||
| const keywordsData = await fetchKeywordsFile(org, site, url.suppliedPath); | ||
| if (keywordsData) { | ||
| const langMetadata = buildLanguageMetadata(keywordsData, langs); | ||
| if (langMetadata && Object.keys(langMetadata).length > 0) { | ||
| url.translationMetadata = langMetadata; | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
nit early return
| if (hasKeywords) { | |
| const keywordsData = await fetchKeywordsFile(org, site, url.suppliedPath); | |
| if (keywordsData) { | |
| const langMetadata = buildLanguageMetadata(keywordsData, langs); | |
| if (langMetadata && Object.keys(langMetadata).length > 0) { | |
| url.translationMetadata = langMetadata; | |
| } | |
| } | |
| } | |
| if (!hasKeywords) return; | |
| const keywordsData = await fetchKeywordsFile(org, site, url.suppliedPath); | |
| if (!keywordsData) return; | |
| const langMetadata = buildLanguageMetadata(keywordsData, langs); | |
| if (langMetadata && Object.keys(langMetadata).length > 0) { | |
| url.translationMetadata = langMetadata; | |
| } |
- HTML annotation for maxlength and keywords based on block-schema (its-storage-size, its-loc-note, its-loc-note-type) - Per-language keywords metadata (langMetadata) sent to GLaaS - Single <p> tag unwrapping for cleaner HTML structure
This reverts commit 21a6b56.
300c93d to
50ea75b
Compare
(its-storage-size, its-loc-note, its-loc-note-type)
<p>tag unwrapping for cleaner HTML structure