Skip to content
Merged
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## 0.8.0

Regenerated output changes for every provider: the front matter `description:` of every resource and service page.

- docgen: per-page descriptions. Resource pages take the first sentence of the resource description (HTML tags and Markdown links stripped, inline code backticks dropped, entities decoded, whitespace collapsed, capped at 160 characters on a word boundary), falling back to `Query, deploy and manage the <resource> resource in the <provider> <service> service using SQL` when the resource has none, so no resource page carries the former provider-wide line and no two share a description. Service index pages read `Query, deploy and manage <provider> <service> resources using SQL`. The provider index page keeps the description from `headerContent1.txt`. Applies to `generateDocs` and `generateDocsv2`
- docgen: front matter descriptions are written as YAML double-quoted scalars (`JSON.stringify`), so a description containing a colon, quotes, `#` or a newline still parses
- new helpers: `metaDescription(text, fallback)`, `yamlString(value)` and `META_DESCRIPTION_MAX` in `src/docgen/helpers.js`; `resourceFallbackDescription(resourceName, providerName, serviceName)` in `src/docgen/resource/overview.js`. Covered by tests/docgen/descriptions.test.js

## 0.7.12

- docgen: `generateDocsv2` (CLI: `generate-docs-v2`) ends each Provider Summary row with the two-space Markdown hard break the v1 template already uses, so `total services`, `total resources` and `source project` render on separate lines instead of one paragraph. v1 output is unchanged. Covered by tests/docgen/summary.test.js
Expand Down
10 changes: 10 additions & 0 deletions docs/docgen.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,16 @@ Resource documentation includes:
- Example queries
- Request and response details

### Page descriptions

Each generated page's front matter `description:` becomes its meta description (and the `WebPage.description` of any structured data built from it), so it is set per page rather than repeated across the site:

- Resource pages: the first sentence of the resource's `description` in the provider spec, with HTML tags and Markdown links stripped (their text kept), inline code backticks dropped, entities decoded and whitespace collapsed, capped at 160 characters on a word boundary (`...` marks a cut). A resource with no description (or one that is empty after cleanup) gets `Query, deploy and manage the <resource> resource in the <provider> <service> service using SQL`, so resource pages never share the provider-wide line or each other's description.
- Service index pages: `Query, deploy and manage <provider> <service> resources using SQL`.
- The provider index page: whatever `headerContent1.txt` sets; docgen does not touch it.

Values are written as YAML double-quoted scalars (`JSON.stringify`), so colons, quotes, `#` and newlines in a description cannot break the front matter. Applies to `generateDocs` and `generateDocsv2`.

## Example Header Content Files

### headerContent1.txt
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@stackql/provider-utils",
"version": "0.7.12",
"version": "0.8.0",
"description": "Utilities for building StackQL providers from OpenAPI specifications.",
"type": "module",
"main": "./src/index.js",
Expand Down
3 changes: 2 additions & 1 deletion src/docgen/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import fs from 'fs';
import path from 'path';
import yaml from 'js-yaml';
import { createResourceIndexContent, createResourceIndexContentv2 } from './resource-content.js';
import { yamlString } from './helpers.js';
import SwaggerParser from '@apidevtools/swagger-parser';
import * as deno_openapi_dereferencer from "@stackql/deno-openapi-dereferencer";

Expand Down Expand Up @@ -262,7 +263,7 @@ keywords:
- infrastructure-as-code
- configuration-as-data
- cloud inventory
description: Query, deploy and manage ${providerName} resources using SQL
description: ${yamlString(`Query, deploy and manage ${providerName} ${serviceName} resources using SQL`)}
custom_edit_url: null
image: /img/stackql-${providerName}-provider-featured-image.png
---
Expand Down
81 changes: 81 additions & 0 deletions src/docgen/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,87 @@ export function getIndefiniteArticle(resourceName) {
return article;
}

// Longest meta description a generated page carries; search engines cut
// the snippet at about this length anyway.
export const META_DESCRIPTION_MAX = 160;

// Words whose trailing period does not end a sentence.
const NON_TERMINAL_ABBREVIATION = /(?:^|[\s(])(?:e\.g|i\.e|etc|vs|approx|incl|cf|inc|ltd|co|no)\.$/i;

const HTML_ENTITIES = { amp: '&', lt: '<', gt: '>', quot: '"', apos: "'", nbsp: ' ' };

function decodeEntities(text) {
return text.replace(/&(#x[0-9a-f]+|#[0-9]+|[a-z]+);/gi, (match, entity) => {
if (entity[0] === '#') {
const code = entity[1] === 'x' || entity[1] === 'X' ? parseInt(entity.slice(2), 16) : parseInt(entity.slice(1), 10);
const valid = Number.isFinite(code) && code > 0 && code <= 0x10ffff && (code < 0xd800 || code > 0xdfff);
return valid ? String.fromCodePoint(code) : match;
}
return HTML_ENTITIES[entity.toLowerCase()] ?? match;
});
}

// The first sentence: up to the first '.', '!' or '?' followed by whitespace
// or the end, skipping abbreviations such as "e.g.". The whole text when no
// sentence end is found.
function firstSentence(text) {
const end = /[.!?](?=\s|$)/g;
let match;
while ((match = end.exec(text)) !== null) {
const sentence = text.slice(0, match.index + 1);
if (!NON_TERMINAL_ABBREVIATION.test(sentence)) return sentence;
}
return text;
}

/**
* The front matter `description:` (the page's meta description) for a
* generated page, derived from a description that may carry HTML and
* Markdown: HTML tags and Markdown links stripped (link text kept), inline
* code backticks and bold markers dropped, entities decoded, whitespace
* collapsed, then the first sentence, capped at META_DESCRIPTION_MAX
* characters on a word boundary ("..." marks a cut).
* @param {string} text - The source description (may be empty)
* @param {string} fallback - Returned as given (not capped) when nothing is
* left after cleanup
* @return {string} - Plain text: at most META_DESCRIPTION_MAX characters
* when derived from `text`, otherwise `fallback`
*/
export function metaDescription(text, fallback) {
const plain = decodeEntities(
String(text ?? '')
.replace(/<!--[\s\S]*?-->/g, ' ')
.replace(/!\[([^\]]*)\]\([^)]*\)/g, '$1')
.replace(/\[([^\]]+)\]\([^)]*\)/g, '$1')
.replace(/<\/?(?:br|p|div|li|ul|ol|tr|td|th|table|tbody|thead|h[1-6]|blockquote|pre)\b[^>]*>/gi, ' ')
.replace(/<\/?[a-z][^>]*>/gi, '')
.replace(/`/g, '')
.replace(/\*\*/g, '')
)
.replace(/\s+/g, ' ')
.trim();
if (!plain) return fallback;

const sentence = firstSentence(plain);
if (sentence.length <= META_DESCRIPTION_MAX) return sentence;
const room = META_DESCRIPTION_MAX - 3;
let cut = sentence.slice(0, room + 1);
const space = cut.lastIndexOf(' ');
cut = space > 0 ? cut.slice(0, space) : sentence.slice(0, room);
return `${cut.replace(/[\s,;:.\-]+$/, '')}...`;
}

/**
* A YAML double-quoted scalar for a front matter value. JSON string syntax
* is a subset of YAML's double-quoted style, so colons, quotes, '#' and
* newlines in the value cannot break the front matter.
* @param {string} value
* @return {string}
*/
export function yamlString(value) {
return JSON.stringify(String(value));
}

/**
* Sanitizes HTML with special handling for allowed tags and backticked content
* @param {string} text - The text to sanitize
Expand Down
17 changes: 15 additions & 2 deletions src/docgen/resource/overview.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
// src/docgen/resource/overview.js
import {
getIndefiniteArticle,
metaDescription,
yamlString,
} from '../helpers.js';

// The page's meta description: the resource description's first sentence
// when there is one, otherwise a line naming the resource, its service and
// the provider, so no two resource pages share a description.
export function resourceFallbackDescription(resourceName, providerName, serviceName) {
return `Query, deploy and manage the ${resourceName} resource in the ${providerName} ${serviceName} service using SQL`;
}

function resourcePageDescription(resourceDescription, resourceName, providerName, serviceName) {
return yamlString(metaDescription(resourceDescription, resourceFallbackDescription(resourceName, providerName, serviceName)));
}

export function createOverviewSection(resourceName, resourceType, resourceDescription, providerName, serviceName) {

let content = `---
Expand All @@ -16,7 +29,7 @@ keywords:
- infrastructure-as-code
- configuration-as-data
- cloud inventory
description: Query, deploy and manage ${providerName} resources using SQL
description: ${resourcePageDescription(resourceDescription, resourceName, providerName, serviceName)}
custom_edit_url: null
image: /img/stackql-${providerName}-provider-featured-image.png
---
Expand Down Expand Up @@ -56,7 +69,7 @@ keywords:
- infrastructure-as-code
- configuration-as-data
- cloud inventory
description: Query, deploy and manage ${providerName} resources using SQL
description: ${resourcePageDescription(resourceDescription, resourceName, providerName, serviceName)}
custom_edit_url: null
image: /img/stackql-${providerName}-provider-featured-image.png
---
Expand Down
Loading