@@ -4,6 +4,11 @@ import { createMessageHelpers } from '@git-stunts/trailer-codec';
44import ContentAddressableStore from '@git-stunts/git-cas' ;
55import VaultResolver from './VaultResolver.js' ;
66import ShellRunner from '@git-stunts/plumbing/ShellRunner' ;
7+ import {
8+ canonicalizeKind ,
9+ canonicalizeSlug ,
10+ resolveContentIdentity ,
11+ } from './ContentIdentityPolicy.js' ;
712
813/**
914 * @typedef {Object } CmsServiceOptions
@@ -43,14 +48,17 @@ export default class CmsService {
4348 * @private
4449 */
4550 _refFor ( slug , kind = 'articles' ) {
46- return `${ this . refPrefix } /${ kind } /${ slug } ` ;
51+ const canonicalSlug = canonicalizeSlug ( slug ) ;
52+ const canonicalKind = canonicalizeKind ( kind ) ;
53+ return `${ this . refPrefix } /${ canonicalKind } /${ canonicalSlug } ` ;
4754 }
4855
4956 /**
5057 * Lists all articles of a certain kind.
5158 */
5259 async listArticles ( { kind = 'articles' } = { } ) {
53- const ns = `${ this . refPrefix } /${ kind } /` ;
60+ const canonicalKind = canonicalizeKind ( kind ) ;
61+ const ns = `${ this . refPrefix } /${ canonicalKind } /` ;
5462 let out = '' ;
5563 try {
5664 out = await this . plumbing . execute ( { args : [ 'for-each-ref' , ns , '--format=%(refname) %(objectname)' ] } ) ;
@@ -84,10 +92,17 @@ export default class CmsService {
8492 * Saves a new version (snapshot) of an article.
8593 */
8694 async saveSnapshot ( { slug, title, body, trailers = { } } ) {
87- const ref = this . _refFor ( slug , 'articles' ) ;
95+ const safeTrailers = trailers && typeof trailers === 'object' ? trailers : { } ;
96+ const identity = resolveContentIdentity ( { slug, trailers : safeTrailers } ) ;
97+ const ref = this . _refFor ( identity . slug , 'articles' ) ;
8898 const parentSha = await this . graph . readRef ( ref ) ;
8999
90- const finalTrailers = { ...trailers , status : 'draft' , updatedAt : new Date ( ) . toISOString ( ) } ;
100+ const finalTrailers = {
101+ ...safeTrailers ,
102+ contentid : identity . contentId ,
103+ status : 'draft' ,
104+ updatedAt : new Date ( ) . toISOString ( ) ,
105+ } ;
91106 const message = this . codec . encode ( { title, body, trailers : finalTrailers } ) ;
92107
93108 const newSha = await this . graph . commitNode ( {
@@ -104,11 +119,12 @@ export default class CmsService {
104119 * Publishes an article by fast-forwarding the 'published' ref.
105120 */
106121 async publishArticle ( { slug, sha } ) {
107- const draftRef = this . _refFor ( slug , 'articles' ) ;
108- const pubRef = this . _refFor ( slug , 'published' ) ;
122+ const canonicalSlug = canonicalizeSlug ( slug ) ;
123+ const draftRef = this . _refFor ( canonicalSlug , 'articles' ) ;
124+ const pubRef = this . _refFor ( canonicalSlug , 'published' ) ;
109125
110126 const targetSha = sha || await this . graph . readRef ( draftRef ) ;
111- if ( ! targetSha ) throw new Error ( `Nothing to publish for ${ slug } ` ) ;
127+ if ( ! targetSha ) throw new Error ( `Nothing to publish for ${ canonicalSlug } ` ) ;
112128
113129 const oldSha = await this . graph . readRef ( pubRef ) ;
114130 await this . repo . updateRef ( { ref : pubRef , newSha : targetSha , oldSha } ) ;
@@ -120,6 +136,7 @@ export default class CmsService {
120136 * Uploads an asset and returns its manifest and CAS info.
121137 */
122138 async uploadAsset ( { slug, filePath, filename } ) {
139+ const canonicalSlug = canonicalizeSlug ( slug ) ;
123140 const ENV = ( process . env . GIT_CMS_ENV || 'dev' ) . toLowerCase ( ) ;
124141 const encryptionKeyRaw = await this . vault . resolveSecret ( {
125142 envKey : 'CHUNK_ENC_KEY' ,
@@ -130,14 +147,14 @@ export default class CmsService {
130147
131148 const manifest = await this . cas . storeFile ( {
132149 filePath,
133- slug,
150+ slug : canonicalSlug ,
134151 filename,
135152 encryptionKey
136153 } ) ;
137154
138155 const treeOid = await this . cas . createTree ( { manifest } ) ;
139156
140- const ref = `refs/_blog/chunks/${ slug } @current` ;
157+ const ref = `refs/_blog/chunks/${ canonicalSlug } @current` ;
141158 const commitSha = await this . graph . commitNode ( {
142159 message : `asset:${ filename } \n\nmanifest: ${ treeOid } ` ,
143160 } ) ;
0 commit comments