@docmd/plugin-seo 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +18 -0
  3. package/index.js +77 -0
  4. package/package.json +7 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 docmd (docmd.io)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,18 @@
1
+ # @docmd/plugin-seo
2
+
3
+ Generates Meta tags, Open Graph (Facebook), and Twitter Card data for **docmd**.
4
+
5
+ ## Configuration
6
+ ```javascript
7
+ // docmd.config.js
8
+ module.exports = {
9
+ plugins: {
10
+ seo: {
11
+ defaultDescription: 'My documentation site',
12
+ twitter: {
13
+ siteUsername: '@myproject'
14
+ }
15
+ }
16
+ }
17
+ }
18
+ ```
package/index.js ADDED
@@ -0,0 +1,77 @@
1
+ /**
2
+ * Generates HTML meta tags for a specific page.
3
+ * @param {Object} config - Project config
4
+ * @param {Object} pageData - { frontmatter, outputPath }
5
+ * @param {string} relativePathToRoot - Path relative to root (for assets)
6
+ * @returns {string} HTML string of meta tags
7
+ */
8
+ function generateMetaTags(config, pageData, relativePathToRoot) {
9
+ let html = '';
10
+ const { frontmatter, outputPath } = pageData;
11
+ const seo = frontmatter.seo || {}; // Page-specific SEO overrides
12
+ const globalSeo = config.plugins?.seo || {};
13
+
14
+ // 1. Robots
15
+ if (frontmatter.noindex || seo.noindex) {
16
+ return '<meta name="robots" content="noindex">\n';
17
+ }
18
+
19
+ // 2. Basic Meta
20
+ const siteTitle = config.siteTitle;
21
+ const pageTitle = frontmatter.title || 'Untitled';
22
+ const description = seo.description || frontmatter.description || globalSeo.defaultDescription || '';
23
+
24
+ html += `<meta name="description" content="${description}">\n`;
25
+
26
+ // 3. Canonical URL
27
+ const siteUrl = config.siteUrl ? config.siteUrl.replace(/\/$/, '') : '';
28
+ // Convert "guide/index.html" -> "/guide/"
29
+ const urlPath = outputPath.replace(/(^|\/)index\.html$/, '$1');
30
+ const pageUrl = `${siteUrl}/${urlPath.replace(/^\//, '')}`;
31
+
32
+ const canonical = seo.canonicalUrl || frontmatter.canonicalUrl || pageUrl;
33
+ if(canonical) {
34
+ html += `<link rel="canonical" href="${canonical}">\n`;
35
+ }
36
+
37
+ // 4. Open Graph (Facebook/LinkedIn)
38
+ html += `<meta property="og:title" content="${pageTitle} : ${siteTitle}">\n`;
39
+ html += `<meta property="og:description" content="${description}">\n`;
40
+ html += `<meta property="og:url" content="${pageUrl}">\n`;
41
+ html += `<meta property="og:type" content="${seo.ogType || frontmatter.ogType || 'website'}">\n`;
42
+
43
+ // Image Logic
44
+ let image = seo.image || frontmatter.image || globalSeo.openGraph?.defaultImage;
45
+ if (image) {
46
+ if (!image.startsWith('http')) {
47
+ // Resolve relative image path to absolute URL
48
+ image = `${siteUrl}/${image.replace(/^\.?\//, '')}`;
49
+ }
50
+ html += `<meta property="og:image" content="${image}">\n`;
51
+ }
52
+
53
+ // 5. Twitter
54
+ const cardType = seo.twitterCard || globalSeo.twitter?.cardType || 'summary_large_image';
55
+ html += `<meta name="twitter:card" content="${cardType}">\n`;
56
+
57
+ if (globalSeo.twitter?.siteUsername) {
58
+ html += `<meta name="twitter:site" content="${globalSeo.twitter.siteUsername}">\n`;
59
+ }
60
+
61
+ html += `<meta name="twitter:title" content="${pageTitle}">\n`;
62
+ html += `<meta name="twitter:description" content="${description}">\n`;
63
+ if (image) {
64
+ html += `<meta name="twitter:image" content="${image}">\n`;
65
+ }
66
+
67
+ // 6. Keywords
68
+ const keywords = seo.keywords || frontmatter.keywords;
69
+ if (keywords) {
70
+ const kwStr = Array.isArray(keywords) ? keywords.join(', ') : keywords;
71
+ html += `<meta name="keywords" content="${kwStr}">\n`;
72
+ }
73
+
74
+ return html;
75
+ }
76
+
77
+ module.exports = { generateMetaTags };
package/package.json ADDED
@@ -0,0 +1,7 @@
1
+ {
2
+ "name": "@docmd/plugin-seo",
3
+ "version": "0.4.0",
4
+ "description": "SEO meta tag generator for docmd",
5
+ "main": "index.js",
6
+ "license": "MIT"
7
+ }