Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Support Flexible Parsing #37

Merged
merged 1 commit into from
Jul 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ export function toCamelCase(name) {
return toClassName(name).replace(/-([a-z])/g, (g) => g[1].toUpperCase());
}

/**
* Removes all leading hyphens from a string.
* @param {String} after the string to remove the leading hyphens from, usually is colon
* @returns {String} The string without leading hyphens
*/
export function removeLeadingHyphens(inputString) {
// Remove all leading hyphens which are converted from the space in metadata
return inputString.replace(/^(-+)/, '');
}

/**
* Retrieves the content of metadata tags.
* @param {String} name The metadata name (or property)
Expand All @@ -94,12 +104,13 @@ export function getMetadata(name) {
*/
export function getAllMetadata(scope) {
const value = getMetadata(scope);
const metaTags = document.head.querySelectorAll(`meta[name^="${scope}-"], meta[property^="${scope}:-"]`);

const metaTags = document.head.querySelectorAll(`meta[name^="${scope}"], meta[property^="${scope}:"]`);
return [...metaTags].reduce((res, meta) => {
const key = meta.getAttribute('name')
? meta.getAttribute('name').substring(scope.length + 1)
: meta.getAttribute('property').substring(scope.length + 2);
const key = removeLeadingHyphens(
meta.getAttribute('name')
? meta.getAttribute('name').substring(scope.length)
: meta.getAttribute('property').substring(scope.length + 1),
);

const camelCaseKey = toCamelCase(key);
res[camelCaseKey] = meta.getAttribute('content');
Expand Down
Loading