Skip to content

Commit a9b4652

Browse files
committed
Fix slug bug and use the most recent hash affecting a file
The changes introduced in this commit should allow for consistent asset URL generation. Meaning, if one attempts to generate eqn src URLs in different commits, as long as the most recent commit hash affecting the asset file is the same, the generated asset URL will be the same. This allows one to update equation src URLs across the project without unnecessarily updating hashes and thus creating commit noise.
1 parent c99feea commit a9b4652

2 files changed

Lines changed: 45 additions & 25 deletions

File tree

  • lib/node_modules/@stdlib/_tools/remark/plugins/remark-img-equations-src-urls/lib

lib/node_modules/@stdlib/_tools/remark/plugins/remark-img-equations-src-urls/lib/git.js

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,18 @@
2020

2121
// MODULES //
2222

23-
var exec = require( 'child_process' ).execSync;
23+
var exec = require( 'child_process' ).execSync; // eslint-disable-line no-sync
2424
var logger = require( 'debug' );
2525
var trim = require( '@stdlib/string/trim' );
26+
var extname = require( '@stdlib/utils/extname' );
2627

2728

2829
// VARIABLES //
2930

3031
var debug = logger( 'remark-img-equations-src-urls:git' );
3132

3233
// Regular expression to extract a repository slug:
33-
var RE = /(?:.+github\.com)(?:\/|:)(.+)(?:\.(?:.+)|$)/;
34+
var RE = /(?:.+github\.com)(?:\/|:)(.+)/;
3435

3536

3637
// MAIN //
@@ -42,16 +43,16 @@ var RE = /(?:.+github\.com)(?:\/|:)(.+)(?:\.(?:.+)|$)/;
4243
* @returns {Object} repository info
4344
*/
4445
function git() {
46+
var branch;
4547
var origin;
46-
var hslug;
47-
var rslug;
48+
var slug;
4849
var opts;
49-
var hash;
5050
var dir;
5151
var cmd;
5252
var out;
53+
var ext;
5354

54-
// Get the local git repository path and remove any newline characters:
55+
// Get the local git repository path:
5556
dir = exec( 'git rev-parse --show-toplevel' );
5657
dir = trim( dir.toString() );
5758
dir = dir.match( /(.+)/ )[ 1 ];
@@ -61,37 +62,36 @@ function git() {
6162
'cwd': dir
6263
};
6364

65+
// Get the current branch:
66+
cmd = 'git rev-parse --abbrev-ref HEAD';
67+
out = exec( cmd, opts );
68+
branch = trim( out.toString() );
69+
debug( 'Branch: %s', branch );
70+
6471
// Get the remote origin:
6572
cmd = 'git config --get remote.origin.url';
6673
out = exec( cmd, opts );
6774
origin = trim( out.toString() );
75+
ext = extname( origin ); // e.g., https://github.com/stdlib-js/stdlib.git
76+
if ( ext ) {
77+
origin = origin.slice( 0, origin.length-ext.length ); // e.g., https://github.com/stdlib-js/stdlib
78+
}
6879
debug( 'Remote origin: %s', origin );
6980

7081
// Extract the repository slug:
71-
rslug = origin.match( RE )[ 1 ];
72-
debug( 'Repository slug: %s', rslug );
73-
74-
// Get the current Git hash and remove any newline characters:
75-
cmd = 'git rev-parse HEAD';
76-
out = exec( cmd, opts );
77-
out = trim( out.toString() );
78-
hash = out.match( /(.+)/ )[ 1 ];
79-
debug( 'Current hash: %s', hash );
80-
81-
hslug = rslug+'/'+hash;
82-
debug( 'Hash slug: %s', hslug );
82+
slug = origin.match( RE )[ 1 ];
83+
debug( 'Repository slug: %s', slug );
8384

8485
out = {
8586
'dir': dir,
86-
'slug': rslug,
87-
'hash': hash,
87+
'slug': slug,
8888
'origin': origin,
89-
'hslug': hslug
89+
'branch': branch
9090
};
9191
return out;
9292
}
9393

9494

9595
// EXPORTS //
9696

97-
module.exports = git();
97+
module.exports = git;

lib/node_modules/@stdlib/_tools/remark/plugins/remark-img-equations-src-urls/lib/transformer.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@
2020

2121
// MODULES //
2222

23+
var exec = require( 'child_process' ).execSync; // eslint-disable-line no-sync
2324
var resolve = require( 'path' ).resolve;
2425
var join = require( 'path' ).join;
2526
var logger = require( 'debug' );
2627
var visit = require( 'unist-util-visit' );
2728
var PATH_SEP = require( '@stdlib/constants/path/sep' );
29+
var trim = require( '@stdlib/string/trim' );
2830
var jsdelivr = require( '@stdlib/_tools/utils/jsdelivr-url' );
2931
var git = require( './git.js' );
3032

@@ -35,6 +37,7 @@ var debug = logger( 'remark-img-equations-src-urls:transformer' );
3537
var DIV_EQN = /<div class="equation"/;
3638
var IMG_SOURCE = /(<img src=")([^"]*)(")/;
3739
var LABEL = /data-equation="eq:([^"]*)">/;
40+
var GIT = git();
3841

3942

4043
// MAIN //
@@ -73,7 +76,11 @@ function factory( opts ) {
7376
var fpath;
7477
var rpath;
7578
var label;
79+
var slug;
80+
var hash;
7681
var url;
82+
var cmd;
83+
var out;
7784

7885
if ( DIV_EQN.test( node.value ) === true ) {
7986
label = LABEL.exec( node.value );
@@ -93,13 +100,26 @@ function factory( opts ) {
93100
fpath = resolve( file.dirname, fpath );
94101
debug( 'Absolute filepath: %s', fpath );
95102

96-
// Get file path relative to git repository:
97-
rpath = fpath.replace( git.dir + PATH_SEP, '' );
103+
// Get file path relative to Git repository:
104+
rpath = fpath.replace( GIT.dir + PATH_SEP, '' );
98105
debug( 'Relative filepath: %s', rpath );
99106

107+
// Get the most recent Git hash affecting the file:
108+
cmd = 'git rev-list -1 '+GIT.branch+' '+fpath;
109+
out = exec( cmd, {
110+
'cwd': GIT.dir
111+
});
112+
out = trim( out.toString() );
113+
hash = out.match( /(.+)/ )[ 1 ];
114+
debug( 'Hash: %s', hash );
115+
116+
// Generate the file path slug:
117+
slug = GIT.slug+'/'+hash;
118+
debug( 'Hash slug: %s', slug );
119+
100120
// Retrieve source URL:
101121
url = jsdelivr({
102-
'slug': git.hslug,
122+
'slug': slug,
103123
'file': rpath
104124
});
105125
debug( 'Source URL: %s', url );

0 commit comments

Comments
 (0)